BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

.NET Framework
.NET 1.1+

Launching Processes From .NET Applications

Some applications require the ability to launch other programs, either to open new windows or to reopen an application with alternative privileges. The .NET framework provides methods that permit you to start .NET and other processes easily.

Launching Processes

There are many reasons why you may want to launch a new process from your .NET application, other than if your program is a utility that is designed to start other programs. For example, you may wish to show a web page in the user's preferred web browser or open a text file containing a log or release information for your new software version. If you have a program that requires administrative privileges, you may decide to run it as a standard user until an administrative function is required. At this point, you may reopen the program, running as administrator, or start a process that handles just the elements that require elevated permissions.

Starting a New Process

The classes and methods that are used to launch new processes are found in the System.Diagnostics namespace. When using these items, it is therefore useful to include the following directive:

using System.Diagnostics;

All of the ways to launch new programs that we will investigate in this article use the Start method of the Process class. The simplest version is a static method that accepts a single string parameter. The parameter contains the name and, optionally, the path of the program or document to open. When executed, the effect is similar to typing the contents of the parameter into the dialog box that appears when you select Run from the Windows Start menu. If a program name is provided, the program is launched. If the name of a document is passed to the parameter, and the type of the document is registered within Windows, the appropriate application is launched and the document is opened.

The following code launches a new instance of the Notepad utility:

Process.Start("notepad.exe");

This example functions correctly because the notepad.exe file can be found within a folder that is automatically searched when starting a process. If this were not the case, you would need to provide the full path to the executable, which would require code similar to the following:

Process.Start(@"c:\windows\notepad.exe");

As described earlier, the parameter does not need to contain the name of an executable file. It can contain a document, web site URL or other item that could be used from the Run dialog box. For example, you can open a web site in the user's preferred web browser using the following code:

Process.Start("http://www.blackwasp.co.uk/");

Starting an Existing Process

The static Start method returns an object of the Process type. This object contains information about the program that was started, including details about the running process and whether it has been exited. We can obtain the Process object by assigning the return value to a variable:

Process p = Process.Start("notepad.exe");

Once the Process object is available, it can be used to obtain details about the process and can also be used to start another similar process using the instance method variation of Start. For example, the following code opens two Notepad windows:

Process p = Process.Start("notepad.exe");
p.Start();

NB: This is not an ideal manner in which to start a process. It would be more usual to create a new Process object and then call its Start method.

Killing a Process

When you start a new process, it uses memory and other resources. In many cases, the new program appears as a window that can be closed when no longer required, freeing those resources. However, some processes do not include any visible elements. If they are not closed, they may continue to hold resources that cannot be cleared by the user. You should be careful to ensure that, if these programs will not be closed by the user or automatically on completion, you close them yourself before exiting your main program.

You can stop a process using the Kill method of the Process class. This is an instance method so requires that you have a Process object that refers to the process to be stopped. To demonstrate, try executing the following code. This opens a new Notepad window, pauses for three seconds and then kills the Notepad process. As this code uses the Thread class, ensure that you add using System.Threading; to the top of the code.

Process p = Process.Start("notepad.exe");
Thread.Sleep(3000);
p.Kill();

One problem with this approach is that if the user has closed the process, the Kill method throws an exception. Try executing the code again but this time close the Notepad window as soon as it appears. The code fails when the Kill method is attempted.

To avoid this problem, always check the HasExited property of the Process object before using Kill. This property returns a Boolean value. If the value is true, the process has already been closed. We should therefore update the code to that shown below. Try executing the new version and closing the Notepad window before it is closed automatically. No exception will be thrown.

Process p = Process.Start("notepad.exe");
Thread.Sleep(3000);
if (!p.HasExited) p.Kill();

Passing Parameters to a New Process

Many programs allow you to specify parameters so that you can control how the program behaves or to provide the name of a document to be loaded. You can recreate this functionality using a third version of the Start method. This time we will use two string parameters. The first parameter accepts the name of the program to be launched and then second provides the arguments to pass to the new process.

We can demonstrate this by executing the following code. This launches a web site within Internet Explorer, even if this is not the user's default web browser.

Process.Start("iexplore.exe", "http://www.blackwasp.co.uk");

Using Process Start Information

The final overloaded version of the Start method that we will consider uses a single parameter. This parameter accepts a ProcessStartInfo object, which contains details of the process to be launched. This object can contain additional information to that which may be supplied using strings alone.

The ProcessStartInfo object can include the filename and arguments information that you previously passed using string parameters. This information should be stored in the FileName and Arguments properties respectively. One of the other properties that can be set is WindowStyle. This determines whether the new program is executed in a maximised, minimised or normal state, or if the program is hidden.

To demonstrate, try executing the following code. This opens a new instance of Notepad in a minimised window.

ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "notepad.exe";
psi.WindowStyle = ProcessWindowStyle.Minimized;
Process.Start(psi);
24 March 2009