CSharp - Essential Types Process class

Introduction

Process class in System.Diagnostics allows you to launch a new process.

static Process.Start method has a number of overloads and the simplest accepts a simple filename with optional arguments:

Process.Start ("notepad.exe");
Process.Start ("notepad.exe", "e:\\file.txt");

You can also specify just a filename, and the registered program for its extension will be launched:

Process.Start ("e:\\file.txt");

You can use ProcessStartInfo to build a command.

ProcessStartInfo psi = new ProcessStartInfo
{
       FileName = "cmd.exe",
       Arguments = "/c ipconfig /all",
       RedirectStandardOutput = true,
       UseShellExecute = false
};
Process p = Process.Start (psi);
string result = p.StandardOutput.ReadToEnd();
Console.WriteLine (result);

You can do the same to invoke the csc compiler, if you set Filename to the following:

psi.FileName = System.IO.Path.Combine (
       System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory(),
       "csc.exe");

If you don't redirect output, Process.Start executes the program in parallel to the caller.

To wait for the new process to complete, use WaitForExit on the Process object with an optional timeout.