CHAPTER 17 PROCESSES, APPDOMAINS, AND OBJECT CONTEXTS
Console.Write("--> Hit enter to kill {0}...", ieProc.ProcessName);
Console.ReadLine();
}
// Kill the iexplore.exe process.
try
{
ieProc.Kill();
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.Message);
}
The static Process.Start() method has been overloaded a few times. At minimum, you will need to
specify the friendly name of the process you want to launch (such as Microsoft Internet Explorer,
iexplore.exe). This example makes use of a variation of the Start() method that allows you to specify
any additional arguments to pass into the program’s entry point (i.e., the Main() method).
After you call the Start() method, you are returned a reference to the newly activated process.
When you want to terminate the process, simply call the instance-level Kill() method. Here, you are
wrapping the calls to Start() and Kill() within a try/catch block, and handling any
InvalidOperationException errors. This is especially important when calling the Kill() method, as this
error will be raised if the process has already been terminated prior to calling Kill().
Controlling Process Startup Using the ProcessStartInfo Class
The Start() method also allows you to pass in a System.Diagnostics.ProcessStartInfo type to specify
additional bits of information regarding how a given process should come to life. Here is a partial
definition of ProcessStartInfo (see the .NET Framework 4.5 SDK documentation for full details):
public sealed class ProcessStartInfo : object
{
public ProcessStartInfo();
public ProcessStartInfo(string fileName);
public ProcessStartInfo(string fileName, string arguments);
public string Arguments { get; set; }
public bool CreateNoWindow { get; set; }
public StringDictionary EnvironmentVariables { get; }
public bool ErrorDialog { get; set; }
public IntPtr ErrorDialogParentHandle { get; set; }
public string FileName { get; set; }
public bool LoadUserProfile { get; set; }
public SecureString Password { get; set; }
public bool RedirectStandardError { get; set; }
public bool RedirectStandardInput { get; set; }
public bool RedirectStandardOutput { get; set; }
public Encoding StandardErrorEncoding { g