0

I put a button in my form that starts the command line application. The application starts but it doesn't show anything. Starting it by just clicking the executable works perfectly. This happens with all command line applications.Why this happens? Here is the code:

Process cmdApp = new Process();

    private void Button9_Click(object sender, EventArgs e)
    {
        richTextBox1.Text = cmdApp.StandardOutput.ReadToEnd();

        cmdApp.WaitForExit();
    }

    private void Button13_Click(object sender, EventArgs e)
    {
        cmdApp.StartInfo.FileName = @"C:\Users\Stelios L LAPTOP\Desktop\steamCMD\steamcmd.exe";
        cmdApp.StartInfo.WorkingDirectory = @"C:\Users\Stelios L LAPTOP\Desktop\steamCMD";
        cmdApp.StartInfo.UseShellExecute = false;
        cmdApp.StartInfo.RedirectStandardOutput = true;
        cmdApp.Start();
    }

Also, when i try reading the output of the command line application, my app just freezes. The most strange thing is that when i close the command line application and try reading its output, i get some lines that all the time they say almost the same thing.

I see someone marked my question as duplicate. NOT IT ISN'T DUPLICATE. The answered question didn't help! Still Nothing. I checked the code million times. I tried everything.Nothing worked!

  • Perhapt this could help you. https://stackoverflow.com/questions/4291912/process-start-how-to-get-the-output – mihai.luca Aug 28 '19 at 11:42
  • no it didn't help me because the first thing i want to do is not to get its output BUT make the command line application show text inside its window! How to get its output if it doesn't show anything in its window! – Randomouss Stuffed Aug 28 '19 at 11:49
  • Try adding process.WaitForExit() before checking the output. – mihai.luca Aug 28 '19 at 12:10
  • I have already tried that. But i don't care about gettings its output because the command line application doesn't show anything in its window! Stop talking about gettings its output with my app. Seeing the text in he command line's application window is more important. I need help desperately – Randomouss Stuffed Aug 28 '19 at 12:19
  • Redirection fundamentally disables output to the console window. It is extra work to collect the output while still having it displayed in the console window; the two are normally mutually exclusive. See marked duplicate for solutions to the general problem, noting of course that they may or may not be viable depending on how much you can change your architecture. – Peter Duniho Aug 28 '19 at 16:17

1 Answers1

0

Setting the RedirectStandardOutput to true means that the standard out will not be written to the console, but instead written to the StandardOutput stream.

If you do not need to capture it in your app, then don't set it to true. By default it is set to false and output is written in the console window.

If you do need both, then you have to write it your self from the StandardOutput stream. You can use the OutputDataReceived event to do that.

Ofir Winegarten
  • 9,215
  • 2
  • 21
  • 27