Reader Level:
ARTICLE

Shell Commands within C#

Posted by Dipal Choksi Articles | Current Affairs July 10, 2002
In this article we will examine a few examples for executing shell commands outside of our program using C#.
  • 0
  • 0
  • 488790

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.

  1. Calculator
  2. DOS Batch program - Map a network drive
  3. Internet explorer with a specified URL
  4. 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 System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace ShellCS_NS
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
public
Form1()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.SuspendLayout();
this.button1.Location = new System.Drawing.Point(16, 24);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(112, 72);
this.button1.TabIndex = 0;
this.button1.Text = "Calculator";
this.button1.Click += new System.EventHandler(this.button1_Click);
this.button2.Location = new System.Drawing.Point(16, 120);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(112, 72);
this.button2.TabIndex = 1;
this.button2.Text = "Map Network Drive";
this.button2.Click += new System.EventHandler(this.button2_Click);
this.button3.Location = new System.Drawing.Point(168, 24);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(112, 72);
this.button3.TabIndex = 2;
this.button3.Text = "Microsoft.com";
this.button3.Click += new System.EventHandler(this.button3_Click);
this.button4.Location = new System.Drawing.Point(168, 120);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(112, 64);
this.button4.TabIndex = 3;
this.button4.Text = "My Word Document";
this.button4.Click += new System.EventHandler(this.button4_Click);
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 213);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button4, this.button3, this.button2, this.button1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
[STAThread]
static void Main()
{
Application.Run(
new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
System.Diagnostics.Process proc =
new System.Diagnostics.Process();
proc.EnableRaisingEvents=
false;
proc.StartInfo.FileName="calc";
proc.Start();
}
private void button2_Click(object sender, System.EventArgs e)
{
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");
}
private void button3_Click(object sender, System.EventArgs e)
{
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");
}
private void button4_Click(object sender, System.EventArgs e)
{
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: Shellcs.cs

NET USE U: \\master\testshare /User:Enterprise\dchoksi passwd /PERSISTENT:YES

Code Listing : Netdrv.bat

COMMENT USING

Trending up