In this article we will examine a few examples for executing shell commands outside of our program using C#. In VB.Net, we can make use of the familiar Shell command to run an executable program. However the Shell function is not available in C#. The Process Class provides access to local and remote processes and enables you to start and stop local system processes.
In our example we will create an example for running the following processes from our program.
- Calculator
- DOS Batch program - Map a network drive
- Internet explorer with a specified URL
- Specified Document in Microsoft Word.
C# Application
Our basic application is a Winforms Visual C# application and will consist of a Windows Form with button controls to invoke the processes specified in the list. (The complete code for the application is provided at the bottom of the article).
Add button controls from the toolbox as shown in the figure below.
Figure 1: Basic Windows Form
Calculator
The option to provide a calculator to the end user on the click of a button can be very useful. The calculator program can be invoked from command line using the command "calc".
Add the following code to the event handler for the button's Click event as shown below. Details explaining the code follow the code listing.
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents=false;
proc.StartInfo.FileName="calc";
proc.Start();
Code Listing: Invoke the calculator from a C# program
We create a new Process and assign the FileName variable to the name of the executable that we want to run. The Start() method of the Process object starts the specified application and assigns it to the process component.
Figure: Calculator is invoked when the Calculator button is clicked.
Map a network Drive
We will invoke a DOS batch script which will map a network drive for us. The DOS command for mapping a drive is as follows:
NET USE <DriveName>: \\<RemoteServer>\<SharedFolder> /User:<Domain>\<UserName> <password> /PERSISTENT:YES
Note : Ensure that the drive mapping is not already in use.
Create a text file containing the above command and Save the file as netdrv.bat. I saved the file in folder c:\dotnetstuff\netdrv.bat.
Here is the format of the command contained in the netdrv.bat file.
NET USE U: \\master\testshare /User:Enterprise\dchoksi passwd /PERSISTENT:YES
Add the following code to the button click event handler of the button labeled "Map Network Drive".
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents=false;
proc.StartInfo.FileName="c:\\dotnetstuff\\netdrv.bat";
proc.Start();
MessageBox.Show("Map Drive Created");
Code Listing: This code will execute the dos batch file and U:\ is mapped to the remote share \\master\testshare.
Microsoft.com
We will create a button that will take the user to the Microsoft.com web site. This logic can be used to open up a registration form for product registration or to point to online help in Windows forms products.
Add the following code in the button Click event handler :
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents=false;
proc.StartInfo.FileName="iexplore";
proc.StartInfo.Arguments=http://www.microsoft.com;
proc.Start();
proc.WaitForExit();
MessageBox.Show("You have just visited www.microsoft.com");
Code Listing: Microsoft.com
Note that the code waits for the Internet Explorer process to exit and then proceeds to display the messagebox. This is achieved through the use of the WaitForExit() function of the Process object.
Open a specific document in Microsoft Word.
Add the following code to the button's OnClick event handler.
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents=false;
proc.StartInfo.FileName="winword";
proc.StartInfo.Arguments="C:\\Dotnetstuff\\TestWordDoc.doc";
proc.Start();
Code Listing : Open specific Word Document
Note that the name of the Word document to open is specified in the Arguments for the StartInfo of the Process.
Complete Code Listing:
using