c# - 为什么在重定向批处理脚本的输出时在 C# 中得到不同的退出代码?

标签 c# process redirectstandardoutput

当我在使用 FC 调用比较两个文件的批处理脚本时重定向 standardOutput 时,与不重定向输出时相比,我得到不同的输出。怎么了?

此代码将在我的日志窗口中打印“0”:

Process test = new Process();
test.StartInfo.FileName = "cmd.exe";
test.StartInfo.Arguments = @"/c fc /b /a C:\temp\debug\1.txt C:\temp\debug\2.txt";
//test.StartInfo.RedirectStandardError = true;
//test.StartInfo.RedirectStandardOutput = true;
test.StartInfo.UseShellExecute = false;
test.StartInfo.CreateNoWindow = true;
test.Start();
test.WaitForExit();
printLog(test.ExitCode.ToString());
return;

此代码将在我的日志窗口中打印“-1”:

Process test = new Process();
test.StartInfo.FileName = "cmd.exe";
test.StartInfo.Arguments = @"/c fc /b /a C:\temp\debug\1.txt C:\temp\debug\2.txt";
test.StartInfo.RedirectStandardError = true;
test.StartInfo.RedirectStandardOutput = true;
test.StartInfo.UseShellExecute = false;
test.StartInfo.CreateNoWindow = true;
test.Start();
test.WaitForExit();
printLog(test.ExitCode.ToString());
return;

最佳答案

我解决了。

我需要将标准输入连同标准错误和标准输出一起重定向。此代码给我 0 作为退出代码:

Process test = new Process();
test.StartInfo.FileName = "fc.exe";
test.StartInfo.Arguments = @"/b /a C:\temp\debug\1.txt C:\temp\debug\2.txt";
test.StartInfo.RedirectStandardError = true;
test.StartInfo.RedirectStandardOutput = true;
test.StartInfo.RedirectStandardInput = true;
test.StartInfo.UseShellExecute = false;
test.StartInfo.CreateNoWindow = true;
test.Start();
test.WaitForExit();
printLog(test.ExitCode.ToString());
return;

如果您在 cmd bash 中编写不带参数的 FC,您将获得 -1 作为退出代码。

关于c# - 为什么在重定向批处理脚本的输出时在 C# 中得到不同的退出代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8247764/

相关文章:

c# - 当您在 Windows 中运行 git push 命令时,控制台的标准输出重定向会发生什么

c# - 如何使用不同的字符编码构建项目?

c# - 如何将 int * 传递给参数类型为 int [] 的函数?

linux - 如何在单个命令行中终止所有 PHP-FPM 进程

swift - Xcode/swift : How to add extra argument to a bash execution

c# - 静默运行时如何捕获 dpinst 的控制台输出?

c# - 如何从 C# 中的方法返回列表

c# - 如何接收UDP并发请求?

c# - 在 C# 中访问 Process.MainModule.FileName 时如何避免 Win32 异常?

linux - 有没有一种方法可以在我的终端屏幕上显示标准输出并重定向到一个文件?