c# – the windowed application does not intercept the console

Question:

Hello, there was such a problem: the Ruby interpreter is installed and if you feed it (via cmd) a file containing nonsense, then of course an error message will be generated. However, if you try to feed this file through a windows forms application, then the error information is not displayed (nothing is displayed at all). What am I doing wrong? Here is the code

namespace RubyIDE
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void runToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.IO.File.WriteAllText("H:\\2.rb", textBox1.Text);

            var psi = new ProcessStartInfo
            {
                FileName = "ruby.exe",
                Arguments = "H:\\2.rb",
                RedirectStandardOutput = true,
                UseShellExecute = false

            };

            var p = Process.Start(psi);
            textBox2.Text = p.StandardOutput.ReadToEnd();
        }

        ...

    }
}

Let me make an explanation, cut the code that is fed to the interpreter is driven into textBox1 and copied to the file H: \ 2.rb, then the interpreter (ruby.exe) is called and an error message is displayed in textBox2 (if any). The problem is that textBox2 remains empty even when there are errors and information about them is printed to the console. In general, you need to make sure that information about errors in the 2.rb script is displayed in textBox2. I would really appreciate your help.

Answer:

Try reading not from StandardOutput but from StandardError . It is possible that the error message is being written to this particular stream.

PS And after having read the output, it is better to wait until the process p.WaitForExit() : p.WaitForExit()

Scroll to Top