2013-04-04 9 views
15

sto eseguendo un comando prompt dei comandi come segue:C# eseguire comandi shell e ottenere il risultato

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

Come ottengo l'output del comando?

+0

Vai a questa risposta di come usare 'ReadToEnd'method: http://stackoverflow.com/a/7160384/214222 –

risposta

19

provare questo

string output = proc.StandardOutput.ReadToEnd(); 
+2

Sì grazie! Ha funzionato! – Mika

+0

come posso ottenere lo stesso risultato se imposto "proc.StartInfo.UseShellExecute = true;"? – anion

+0

Questo non acquisisce l'output quando si verifica un errore. – Arjang

Problemi correlati