0

My issue is that i get no output when i execute the given command.
The exe takes quite a while to finish and while it's executing im expecting a continuous flow of data being outputed. But for some reason it seems my program simply ignores the output specifically from the exe.

I feel like i've tried everything and with no luck.
I've tried other commands such as "tree c:/" and im able to receive that output just fine. Im certain the command im executing is valid. I have tested the command by itself in a seperate cmd and it works without any problems.

I have also tried running the process on only the exe. This also yielded to output.

I've been stuck with this for a while now, so any help would be much appreciated.

Task.Run(() =>
    {
        string command = downloadPath + @"\myinstall.exe quickinstallall";
        var startInfo = new ProcessStartInfo("cmd.exe")
        {
              WorkingDirectory = downloadPath,
              UseShellExecute = false,
              RedirectStandardInput = true,
              RedirectStandardError = true,
              RedirectStandardOutput = true,
              WindowStyle = ProcessWindowStyle.Normal,
              CreateNoWindow = false,

              Verb = "runas"                
        };
        Process cmd = new Process { StartInfo = startInfo };
        cmd.EnableRaisingEvents = true;
        cmd.OutputDataReceived += UpdateOutput;
        cmd.ErrorDataReceived += UpdateOutput;             
        cmd.Start();
        cmd.BeginOutputReadLine();
        cmd.StandardInput.WriteLine(command);
        cmd.BeginErrorReadLine();
        cmd.WaitForExit();
    });
Chris
  • 51
  • 1
  • 5
  • Why on earth are you invoking `cmd` and then laboriously feeding it a command to execute, instead of just invoking `myinstall.exe` directly with the appropriate argument list? – Jeroen Mostert May 17 '22 at 08:44
  • @JeroenMostert I have actually also tried that without any luck as well. – Chris May 17 '22 at 08:45
  • 1
    Is the `runas` because the tool requests elevation? If so, I'm fairly certain you simply can't redirect streams, as a security measure. Redirecting the `cmd` streams would do nothing since elevating creates a new window anyway. – Jeroen Mostert May 17 '22 at 08:52
  • @JeroenMostert Hmm i didn't think about that. I have tried without `runas` and that didn't work for me either. – Chris May 17 '22 at 08:55
  • `WorkingDirectory` is supposed to be `Arguments`. Also check what `Process.EnableRaisingEvents` is for. See the notes and code here: [How do I get output from a command to appear in a control on a Form in real-time?](https://stackoverflow.com/a/51682585/7444103) – Jimi May 17 '22 at 10:16

1 Answers1

0

Okay, so i figured out why the output wasn't being recorded. I was simply missing 2 lines of code...

 cmd.StandardInput.Flush();
 cmd.StandardInput.Close();

AND

I had to remove runas and instead inherit the admin perms from the application itself. By "inherit the admin perms" i mean simply to make sure the application it always being executed as administrator.

I have no idea why this exactly fixes my problem BUT it seems to work perfectly now. If anyone knows why this works i'd love to know.

Chris
  • 51
  • 1
  • 5