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

标签 c# process cmd

代码如下:

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/

相关文章:

c# - 如何防止多次打开我的应用程序?

java - 使用 Java 执行命令行程序时仅收到 null

java - 检测/控制过程

windows - 在不回显命令的情况下使用批处理文件超时?

windows - 当两者具有相同的路径/名称时,如何先执行 .bat 文件而不是文件 .exe?

c# - C# 的部分类是糟糕的设计吗?

C# - 获取引用的引用

c# - 在 C# 中将像素数组转换为图像

file - 为什么我的 Go 程序使用打开文件的名称创建另一个 Go 进程,为什么它这么大?

batch-file - 为什么当前时间不在 FOR 循环中更新?