How can I run another application or batch file from my Visual C# .NET code?

Avatar

Posted by: Duncan Mackenzie, MSDN
This post applies to Visual C# .NET 2002/2003


Suppose you want to run a command line application, open up another Windows program, or even bring up the default web browser or email program… how can you do this from your C# code?


The answer for all of these examples is the same, you can use the classes and methods in System.Diagnostics.Process to accomplish these tasks and more.


Example 1. Running a command line application, without concern for the results:


Example 2. Retrieving the results and waiting until the process stops (running the process synchronously):


 Example 3. Displaying a URL using the default browser on the user’s machine:


In my opinion, you are much better off following the third example for URLs, as opposed to executing IE with the URL as an argument. The code shown for Example 3 will launch the user’s default browser, which may or may not be IE; you are more likely to provide the user with the experience they want and you will be taking advantage of the browser that is most likely to have up-to-date connection information.


C# code download available from http://www.duncanmackenzie.net/Samples/default.aspx


By the way, this post is a simple port of an earlier VB FAQ post… just in case you thought you had seen it already in your feeds…

Avatar

Follow    

No Comments.