c# execute shell command and get result

Mika picture Mika · Apr 4, 2013 · Viewed 28.2k times · Source

I am executing a command prompt command as follows:

string cmd = "/c dir" ; 
System.Diagnostics.Process proc = new System.Diagnostics.Process(); 
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.Arguments = cmd; 
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true; 
proc.Start();

How to I get the output of the command?

Answer

Sachin picture Sachin · Apr 4, 2013

try this

string output = proc.StandardOutput.ReadToEnd();