How To: Starting Processes From C#

by Joe Mayo, 2/24/02

Introduction

A C# program can launch another program using the Process class.  The Process class is part of the System.Diagnostics namespace.  You start another program by instantiating a Process object, setting members of it's StartInfo property, and invoking it's Start() method.  Listing 1 shows how to start a process from C#.

Listing 1: Starting a Process: ProcessStart.cs

using System;
using System.Diagnostics;

namespace csharp_station.howto
{
    /// <summary>
    ///
Demonstrates how to start another program from C#
    /// </summary>
    class ProcessStart
    {
        static void Main(string[] args)
        {
            Process notePad =
new Process();

            notePad.StartInfo.FileName   = "notepad.exe";
            notePad.StartInfo.Arguments = "ProcessStart.cs";

            notePad.Start();
        }
    }
}

When the program in Listing 1 runs, it will open the Windows (TM) notepad application in editing mode on the ProcessStart.cs file.  The first thing the program does is instantiate a Process object called notePad, an identifier that happens to have the same name as the program that will be executed, as follows:

            Process notePad = new Process();

Next, we specify what program will be executed.  This happens by assigning the program name to the FileName member of the notePad Process object's StartInfo property.  Here's the line from the code:

            notePad.StartInfo.FileName   = "notepad.exe";

The StartInfo property also has a member called Arguments, which is used to specify command-line options that are passed to the program.  In the following example, the string ProcessStart.cs is passed as an argument, so the notepad application will know what file to open when it is executed:

            notePad.StartInfo.Arguments = "ProcessStart.cs";

The program is then executed by invoking the Start() method, as shown here:

            notePad.Start();

The results of this program are the same as if the instructions below were typed on the command-line:

           C:>notepad ProcessStart.cs

Summary

This article showed how to start a new process from C#.  This is handy when you want to launch another program from your application.  The example shown, demonstrated how to open the ProcessStart.cs code file with the notepad application.