Search code examples
c#shellcommandoutputprompt

c# execute shell command and get result


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?


Solution

  • try this

    string output = proc.StandardOutput.ReadToEnd();