अजपा योग क्रिया आणि ध्यान : श्वास, मंत्र, मुद्रा आणि ध्यान यांच्या सहाय्याने मनःशांती, एकाग्रता, चक्र संतुलन आणि कुंडलिनी जागृती. अधिक माहिती आणि आगामी तारखांसाठी येथे जा.


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:
  • Navigating to a web URL : The Start() method we just saw also accepts file paths or URLs. In such cases a process is started for the application to which the requested document type is associated. For example to open IE and navigate to http://www.microsoft.com you will write some thing like this
    Process.Start("http://www.microsoft.com");
    
  • Open default Email application : You can use same technique to open your default email application.
    Process.Start("mailto:somebody@somedomain.com");
    

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

Bipin Joshi is an independent software consultant and trainer by profession specializing in Microsoft web development technologies. Having embraced the Yoga way of life he is also a meditation teacher and spiritual guide to his students. He is a prolific author and writes regularly about software development and yoga on his websites. He is programming, meditating, writing, and teaching for over 27 years. To know more about his ASP.NET online courses go here. More details about his Ajapa Japa and Shambhavi Mudra online course are available here.

Posted On : 12 January 2002