Home > Software design >  C# Unable to read output from .exe when executing
C# Unable to read output from .exe when executing

Time:05-20

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();
    });

CodePudding user response:

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.

  • Related