Introduction
It is normal practice to open the Windows command prompt and execute commands. The command when executed shows the result onto the screen. There are many commands that we execute daily such as dir, find, etc. A situation may arise when you want to execute a (shell) command from the C# application.
Don't worry!!! Here is the code to do so…
Using the Code
The code given below creates a process i.e. a command process and then invokes the command that we want to execute. The result of the command is stored in a string
variable, which can then be used for further reference. The command execution can happen in two ways, synchronously and asynchronously. In the asynchronous command execution, we just invoke the command execution using a thread that runs independently. The code has enough comments, hence making it self-explanatory.
Below is the code to execute the command synchronously:
public void ExecuteCommandSync(object command)
{
try
{
System.Diagnostics.ProcessStartInfo procStartInfo =
new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
string result = proc.StandardOutput.ReadToEnd();
Console.WriteLine(result);
}
catch (Exception objException)
{
}
}
The above code invokes the cmd
process specifying the command to be executed. The option procStartInfo.RedirectStandardOutput
is set to true
, since we want the output to be redirected to the StreamReader
. The procStartInfo.CreateNoWindow
property is set to true
, as we don't want the standard black window to appear. This will execute the command silently.
Below is the code to execute the command asynchronously:
public void ExecuteCommandAsync(string command)
{
try
{
Thread objThread = new Thread(new ParameterizedThreadStart(ExecuteCommandSync));
objThread.IsBackground = true;
objThread.Priority = ThreadPriority.AboveNormal;
objThread.Start(command);
}
catch (ThreadStartException objException)
{
}
catch (ThreadAbortException objException)
{
}
catch (Exception objException)
{
}
}
If we observe carefully, the asynchronous execution of the command actually invokes the synchronous command execution method using a thread. The thread runs in the background making the command execution asynchronous in nature.
In the above execution sample, we find that there are two result sets of the command "dir
". The first one appears immediately after the command and the second appears after the "Done!" statement. In this case, the first one is the synchronous execution of the command, which happens immediately and the second is the asynchronous execution of the "dir
" command.
Points of Interest
I always thought of having some code that will execute my DOS commands, finally I had to build it.
You can find some more interesting stuff here.
History
- 12th May, 2008: Initial post