我需要使用进程获取程序的版本。它在我的 WinForms 应用程序中工作,但当我将它放入测试中时,它总是返回 null。我在 NUnit 和 XUnit 中尝试过,但没有成功。
using(var p = new Process()) {
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = "executable.exe";
p.StartInfo.Arguments = "--version";
p.OutputDataReceived += (s, e) => Console.WriteLine(e.Data);
p.ErrorDataReceived += (s, e) => Console.WriteLine(e.Data);
p.Start();
p.BeginOutputReadLine();
p.WaitForExit();
}
我不想复活这个SO问题因为它已经有十年了。