Executing External Applications From Your .NET Application
Introduction
In some cases you need to execute some external application from your own
application. The common candidates for such task are:
-
Running BCP in SQL Server
-
Running batch scripts that automate some tasks
-
Start some add-in application like text editor
-
Opening read me kind of files at the end of installation
.NET provides an easy way to execute a process external to your application. In
this small article we will see how to do that.
Example 1
In this example we will create a console application and see the most basic way
of starting an external application.
namespace ConsoleApplicationCS
{
class Class1
{
static void Main(string[] args)
{
System.Diagnostics.Process.Start("notepad.exe");
}
}
}
The static method - Start of System.Dignostic.Process does the work of starting
notepad. Note that since notepad.exe is found on path environment variable we
need not give its complete path. For other applications you will have to give
full path of the application.
Note : System.Diagnostics also provides classes that allow you read and
write to event logs, and monitor system performance using performance
counters.
Example 2
In this example we will see how to pass command line arguments to the
application
namespace ConsoleApplicationCS
{
class Class1
{
static void Main(string[] args)
{
System.Diagnostics.Process.Start("notepad.exe","sample.txt");
}
}
}
Here, we passed command line arguments for the external application
(notepad.exe in our case) via the second argument of the Start method
Example 3
In previous examples you must have noticed that your applications goes on once
the process is started. It will not wait for the process to finish. Suppose
that you are running SQL Server BCP via Start method and want to process the
uploaded data later in your application then your code should not be executed
till BCP is finished. In this example we will see how to wait for the process
to finish its execution
namespace ConsoleApplicationCS
{
class Class1
{
static void Main(string[] args)
{
System.Diagnostics.Process p=
System.Diagnostics.Process.Start("notepad.exe","sample.txt");
p.WaitForExit();
}
}
}
Actually the Start method returns a reference to the process it starts. The
reference is nothing but an instance of Process class. The WaitForExit() method
of the instance will 'hold' our application from going further. You can also
specify milliseconds to wait for the process if needed.
Example 4
In addition to above, we might need to set parameters like working directory
and window style. The ProcessInfo class allows you to do just that. Following
code shows its usage:
namespace ConsoleApplicationCS
{
class Class1
{
static void Main(string[] args)
{
System.Diagnostics.ProcessStartInfo psi=
new System.Diagnostics.ProcessStartInfo();
psi.FileName="notepad.exe";
psi.Arguments="sample.txt";
psi.WorkingDirectory="c:\\mywork";
psi.WindowStyle=System.Diagnostics.
ProcessWindowStyle.Maximized;
System.Diagnostics.Process p=
System.Diagnostics.Process.Start(psi);
}
}
}
Here, we set the initial folder or working directory for the application. We
also set the window style so that the application will start in maximized
window. If you want to hide the process window from the user (very useful for running batch scripts) you can set the window style to hidden.
Some Common Uses
Here are some illustrations of how you can put your knowledge at work:
Killing a Process
In some rare cases you may need to kill the process you started. You can use
Kill() method of the process instance as shown below:
System.Diagnostics.Process p=
System.Diagnostics.Process.Start("notepad.exe","sample.txt");
//some other code
p.Kill();