-----------------------------------------------------------

New Message on BDOTNET

-----------------------------------------------------------
From: saileshsaha
Message 4 in Discussion


Hi!
 
Please go through 
the following. This will help you.
 
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.


<V:SHAPETYPE id=_x0000_t75 coordsize="21600,21600" o:spt="75" 
o:preferrelative="t" path="[EMAIL PROTECTED]@[EMAIL PROTECTED]@[EMAIL 
PROTECTED]@[EMAIL PROTECTED]@5xe" filled="f" 
stroked="f"><V:STROKE joinstyle="miter"></V:STROKE><V:FORMULAS><V:F 
eqn="if lineDrawn pixelLineWidth 0"></V:F><V:F eqn="sum @0 1 0"></V:F><V:F 
eqn="sum 0 0 @1"></V:F><V:F eqn="prod @2 1 2"></V:F><V:F 
eqn="prod @3 21600 pixelWidth"></V:F><V:F 
eqn="prod @3 21600 pixelHeight"></V:F><V:F eqn="sum @0 0 1"></V:F><V:F 
eqn="prod @6 1 2"></V:F><V:F eqn="prod @7 21600 pixelWidth"></V:F><V:F 
eqn="sum @8 21600 0"></V:F><V:F eqn="prod @7 21600 pixelHeight"></V:F><V:F 
eqn="sum @10 21600 0"></V:F></V:FORMULAS><V:PATH o:extrusionok="f" 
gradientshapeok="t" o:connecttype="rect"></V:PATH><O:LOCK v:ext="edit" 
aspectratio="t"></O:LOCK></V:SHAPETYPE><V:SHAPE 
id=_x0000_i1025 style="WIDTH: 6in; HEIGHT: 255pt" 
type="#_x0000_t75"><V:IMAGEDATA 
src="file:///C:/DOCUME~1/mahesh/LOCALS~1/Temp/msoclip1/01/clip_image001.png" 
o:title=""></V:IMAGEDATA></V:SHAPE><V:SHAPETYPE 
id=_x0000_t75 coordsize="21600,21600" o:spt="75" o:preferrelative="t" 
path="[EMAIL PROTECTED]@[EMAIL PROTECTED]@[EMAIL PROTECTED]@[EMAIL PROTECTED]@5xe" 
filled="f" stroked="f"><V:STROKE 
joinstyle="miter"></V:STROKE><V:FORMULAS><V:F 
eqn="if lineDrawn pixelLineWidth 0"></V:F><V:F eqn="sum @0 1 0"></V:F><V:F 
eqn="sum 0 0 @1"></V:F><V:F eqn="prod @2 1 2"></V:F><V:F 
eqn="prod @3 21600 pixelWidth"></V:F><V:F 
eqn="prod @3 21600 pixelHeight"></V:F><V:F eqn="sum @0 0 1"></V:F><V:F 
eqn="prod @6 1 2"></V:F><V:F eqn="prod @7 21600 pixelWidth"></V:F><V:F 
eqn="sum @8 21600 0"></V:F><V:F eqn="prod @7 21600 pixelHeight"></V:F><V:F 
eqn="sum @10 21600 0"></V:F></V:FORMULAS><V:PATH o:extrusionok="f" 
gradientshapeok="t" o:connecttype="rect"></V:PATH><O:LOCK v:ext="edit" 
aspectratio="t"></O:LOCK></V:SHAPETYPE><V:SHAPE id=_x0000_i1025 
style="WIDTH: 225pt; HEIGHT: 180.75pt" type="#_x0000_t75"><V:IMAGEDATA 
src="file:///C:/TEMP/msoclip1/01/clip_image001.png" 
o:title="figure1"></V:IMAGEDATA></V:SHAPE>

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.


<V:SHAPETYPE 
id=_x0000_t75 coordsize="21600,21600" o:spt="75" o:preferrelative="t" 
path="[EMAIL PROTECTED]@[EMAIL PROTECTED]@[EMAIL PROTECTED]@[EMAIL PROTECTED]@5xe" 
filled="f" stroked="f"><V:STROKE 
joinstyle="miter"></V:STROKE><V:FORMULAS><V:F 
eqn="if lineDrawn pixelLineWidth 0"></V:F><V:F eqn="sum @0 1 0"></V:F><V:F 
eqn="sum 0 0 @1"></V:F><V:F eqn="prod @2 1 2"></V:F><V:F 
eqn="prod @3 21600 pixelWidth"></V:F><V:F 
eqn="prod @3 21600 pixelHeight"></V:F><V:F eqn="sum @0 0 1"></V:F><V:F 
eqn="prod @6 1 2"></V:F><V:F eqn="prod @7 21600 pixelWidth"></V:F><V:F 
eqn="sum @8 21600 0"></V:F><V:F eqn="prod @7 21600 pixelHeight"></V:F><V:F 
eqn="sum @10 21600 0"></V:F></V:FORMULAS><V:PATH o:extrusionok="f" 
gradientshapeok="t" o:connecttype="rect"></V:PATH><O:LOCK v:ext="edit" 
aspectratio="t"></O:LOCK></V:SHAPETYPE><V:SHAPE id=_x0000_i1025 
style="WIDTH: 6in; HEIGHT: 255pt" type="#_x0000_t75"><V:IMAGEDATA 
src="file:///C:/DOCUME~1/mahesh/LOCALS~1/Temp/msoclip1/01/clip_image001.png" 
o:title=""></V:IMAGEDATA></V:SHAPE><V:SHAPETYPE 
id=_x0000_t75 coordsize="21600,21600" o:spt="75" o:preferrelative="t" 
path="[EMAIL PROTECTED]@[EMAIL PROTECTED]@[EMAIL PROTECTED]@[EMAIL PROTECTED]@5xe" 
filled="f" stroked="f"><V:STROKE 
joinstyle="miter"></V:STROKE><V:FORMULAS><V:F 
eqn="if lineDrawn pixelLineWidth 0"></V:F><V:F eqn="sum @0 1 0"></V:F><V:F 
eqn="sum 0 0 @1"></V:F><V:F eqn="prod @2 1 2"></V:F><V:F 
eqn="prod @3 21600 pixelWidth"></V:F><V:F 
eqn="prod @3 21600 pixelHeight"></V:F><V:F eqn="sum @0 0 1"></V:F><V:F 
eqn="prod @6 1 2"></V:F><V:F eqn="prod @7 21600 pixelWidth"></V:F><V:F 
eqn="sum @8 21600 0"></V:F><V:F eqn="prod @7 21600 pixelHeight"></V:F><V:F 
eqn="sum @10 21600 0"></V:F></V:FORMULAS><V:PATH o:extrusionok="f" 
gradientshapeok="t" o:connecttype="rect"></V:PATH><O:LOCK v:ext="edit" 
aspectratio="t"></O:LOCK></V:SHAPETYPE><V:SHAPE id=_x0000_i1025 
style="WIDTH: 196.5pt; HEIGHT: 189pt" type="#_x0000_t75"><V:IMAGEDATA 
src="../../../images/ShellC2.gif" 
o:title="figure2"></V:IMAGEDATA></V:SHAPE>

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(); 
}
}
}

 

Hope this will solve 
your problem.

Thanks and 
Regards,

Sailesh

 

-----------------------------------------------------------

To stop getting this e-mail, or change how often it arrives, go to your E-mail 
Settings.
http://groups.msn.com/bdotnet/_emailsettings.msnw

Need help? If you've forgotten your password, please go to Passport Member Services.
http://groups.msn.com/_passportredir.msnw?ppmprop=help

For other questions or feedback, go to our Contact Us page.
http://groups.msn.com/contact

If you do not want to receive future e-mail from this MSN group, or if you received 
this message by mistake, please click the "Remove" link below. On the pre-addressed 
e-mail message that opens, simply click "Send". Your e-mail address will be deleted 
from this group's mailing list.
mailto:[EMAIL PROTECTED]

Reply via email to