0

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.

miapuffia
  • 31
  • 4
  • What is returned null? – Chetan May 04 '23 at 01:45
  • Try `p.StartInfo.RedirectStandardInput = false`. The following may be helpful: https://stackoverflow.com/a/71344930/10024425 (you can ignore the _Administrator_ portion in the beginning of the post; also ignore _psInfo.Verb = "runas"_ ). – Tu deschizi eu inchid May 04 '23 at 01:46
  • There are other properties that may need to be set. You'll need to refer to the [Process](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process?view=net-8.0) documentation. I recommend that you look at the properties that are set in the post that I referenced in my previous comment and then read the documentation for each one of them. – Tu deschizi eu inchid May 05 '23 at 14:36

0 Answers0