2013-04-04 69 views
15

我执行命令提示符下命令如下:C#执行shell命令并获得结果

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

如何我得到命令的输出?

+0

查看如何使用'ReadToEnd'method这样的回答:http://stackoverflow.com/a/7160384/214222 – 2013-04-04 20:31:41

回答

19

试试这个

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

好的,谢谢!有效! – Mika 2013-04-06 05:11:29

+0

如何设置“proc.StartInfo.UseShellExecute = true;”我如何得到相同的结果? – anion 2017-06-22 13:25:27

+0

当出现错误时,这不会捕获输出。 – Arjang 2017-06-26 23:11:52