Introduction
A C# program can launch another program using the Process class. The Process class is part of the System.Diagnostics namespace. You start another program by instantiating a Process object, setting members of it's StartInfo property, and invoking it's Start() method.
Main
We can use System.Diagnostics. Our exsample below will show you how to start a windows command prompt using C# Syntax.
private string ExecuteProcess(string ProcessFileName, string ProcessArguments)
{
string Result = "";
Process oProcess = null;
try
{
ProcessStartInfo oInfo =
new ProcessStartInfo(ProcessFileName, ProcessArguments);
oInfo.CreateNoWindow = true;
oInfo.UseShellExecute = false;
oInfo.RedirectStandardError = true;
oInfo.RedirectStandardOutput = true;
oInfo.RedirectStandardInput = true;
oProcess = Process.Start(oInfo);
oProcess.WaitForExit(20000);
if (!oProcess.HasExited)
{
oProcess.Kill();
}
Result = oProcess.StandardError.ReadToEnd();
}
catch (Exception ex)
{
Result = ex.ToString();
}
finally
{
oProcess.Close();
}
return Result;
}
When the syntax above runs, it will execute the Windows process specify in processFileName. The first thing the program does is instantiate a Process object, an identifier that happens to have the same name as the program that will be executed. We specify what program will be executed. This happens by assigning the program name to the FileName member of the Process object's StartInfo property. Here's the line from the code:
ProcessStartInfo oInfo =
new ProcessStartInfo(ProcessFileName, ProcessArguments);
The StartInfo property also has a member called Arguments, which is used to specify command-line options that are passed to the program. In the above example, the string ProcessArguments is passed as an argument, so the application will know what file to open when it is executed:
The program is then executed by invoking the Start() method. Then next syntax is to specify the process windows will be shown or not. RedirectStandardError, RedirectStandardOutput, and RedirectStandardInput is to show output information of the process whether it is success executed or failed with error message.
Abouve syntax will be run ok, but sometimes depends of your process, we need to change some of the parameter in it. If you want to do some research, then can change it for example createnewwindow or useshellexecute.
Next we will look at the method to execute the windows process itself.
public string ExecuteCommand()
{
string s = ExecuteProcess("cmd.exe", @"notepad.exe");
return s;
}
The results of this program are the same as if the instructions below were typed on the command-line:
C:\notepad.exe
Conclusion
This article showed how to start a new process from C#. This is usefull when you want to launch another program from your application. The example shown, demonstrated how to open the run command prompt to execute another process.
References
Include all the useful links or references that can help users learn about this tutorial
- Managing Windows Process
- Monitoring and Managing Windows Process
Other Related and Popular Articles :
Author Profile : Hans Candra
How would you rate the quality of this content?
Poor
Excellent
Comments
Leave New Comments
Article Content copyright by
Hans Candra
Everything else Copyright © by
WorldofASP.NET
2009