代码如下:
ProcessStartInfo startInfo = new ProcessStartInfo("cmd", "/c" + command);
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.WindowStyle = ProcessWindowStyle.Normal;
startInfo.Arguments = arguments;
startInfo.RedirectStandardError = true;
startInfo.RedirectStandardOutput = true;
Process process = Process.start(startInfo);
StreamReader srOutput = process.StandardOutput;
string output = srOutput.ReadToEnd();
命令是rmdir/s/q 123
我希望在变量 output
中得到“系统找不到指定的文件”,因为“123”是一个不存在的文件路径。但是 output
是一个空字符串。为什么以及如何获取输出?
最佳答案
您希望看到的消息将出现在StandardError
上,而不是StandardOutput
。
关于c# - 无法从 cmd.exe 执行中获取输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6210943/