无法从cmd.exe执行中获取输出

本文关键字:获取 输出 执行 exe cmd | 更新日期: 2023-09-27 17:58:08

代码如下:

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是一个空字符串。为什么以及如何获取输出?

无法从cmd.exe执行中获取输出

您期望看到的消息将在StandardError上,而不是StandardOutput上。