BlackWaspTM
.NET Framework
.NET 1.1+

Launching Processes From .NET Applications

by Richard Carr, published at http://www.blackwasp.co.uk/LaunchProcess.aspx

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.

24 March 2009