I need to get the version of a program using a process. It works in my WinForms app but when I put it in a test it always returns null. I've tried in NUnit and XUnit with no luck.
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();
}
I don't want to resurrect this SO question as it's a decade old.
Edit
Changing p.StartInfo.RedirectStandardInput = false;
had no effect.
To clarify the returns null part, this version:
using(var p = new Process()) {
p.StartInfo.RedirectStandardInput = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = "executable.exe";
p.StartInfo.Arguments = "--version";
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
console.WriteLine(output);
}
actually outputs an empty string, not null.