当我在使用 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/