Launching and Viewing Processes in C#
You can launch a new process from a C# application by using the Process class located in the System.Diagnostics namespace. The Process class also allows you to obtain a collection of all running processes on the system.
How to Launch a New Process
Starting a process is really easy.
Process.Start( "Notepad.exe" );
And that’s it. If Notepad.exe exists on your system then it will start. Make sure that if you do not specify a complete path then the name you do present is available in your environment path. You can wait for the newly created process to terminate as well.
Process p = Process.Start( "Notepad.exe"); p.WaitForExit();
Keep in mind that WaitForExit will block the calling thread. If you don’t want that behavior considering launching the new process on a thread of its own. You can also use the ProcessStartInfo class to pass information to the Start method. This example launches the new process on a thread from the thread pool via an anonymous method. Remember to include System.Threading.
// launch the program / process on a new thread from the thread pool ThreadPool.QueueUserWorkItem( (WaitCallback)delegate(object state) { ProcessStartInfo psi = new ProcessStartInfo("Notepad.exe", ""); Process p = Process.Start(psi); p.WaitForExit(); });
Passing Arguments to the New Process
You can also pass arguments to a new process. The next example will launch Notepad and tell it to open the file MyFile.txt.
ProcessStartInfo psi = new ProcessStartInfo("Notepad.exe", @"C:\My\Path\MyFile.txt"); Process p = Process.Start(psi);
Launching a Document and Setting the Verb
There are some other interesting things you can do with the Process class. For instance, instead of specifying a file name in Process.Start like we did above with Notepad.exe, we could specify a file or document. The method would then open that file with the registered default application for that type of file. You can also specify a verb that is associated with that file type. A verb represents an action that is associated with that file type. So, if I wanted to launch the default application for a .txt file and have that file printed I could do the following:
ProcessStartInfo psi = new ProcessStartInfo(@"C:\My\Path\MyFile.txt"); psi.Verb = "print"; Process p = Process.Start(psi); p.WaitForExit();
Listing All Running Processes
The last thing I want to show you is how easy it is to view a list of all processes running on the system with C#. Assuming you have a ListBox name listbox1, the following code fragment would add an entry to the ListBox with the name of each process running on the system.
Process[] processes = Process.GetProcesses(); foreach( Process process in processes ) listBox1.Items.Add( process.ProcessName );
Well, that’s it for today. Hope this information comes in handy for some of you!
April 15, 2009
Posted in: C#, Programming

3 Responses
Hello,
Everything dynamic and very positively!
Rufor
Thank you! I would now go on this blog every day!
Joker
People in the world take the lowest-rate-loans.com in different creditors, just because this is easy.
Leave a Reply