1

Lots of answers to other questions tell you that you can capture the output of a process using code looking something like this:

public async Task Run()
{
    await Task.Run(() =>
    {
        using (Process process = new Process()
        {
            StartInfo = new ProcessStartInfo
            {
                WindowStyle = ProcessWindowStyle.Normal,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                FileName = "cmd",
                Arguments = "path/filename.bat",
            }
        })
        {
            process.OutputDataReceived += Process_OutputDataReceived;
            process.ErrorDataReceived += Process_ErrorDataReceived;
            process.Start();
            process.BeginOutputReadLine();
            process.BeginErrorReadLine();

            process.WaitForExit();
        }
    });
}

private void Process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
    if (e.Data != null)
    {
        log.Error(e.Data);
    }
}

private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
    if (e.Data != null)
    {
        log.Info(e.Data);
    }
}

There are lots of variants which achieve the same thing more or less but they all redirect the standard output/errors - is there a way to capture them while still having them show in the cmd window?

Toby Smith
  • 1,505
  • 2
  • 15
  • 24
  • Why do you need to show the output from the original terminal if your program is redirecting it? Can you just create a control on your program that displays the child process' output, or even create your own console window if you want it to look the same? – Technoguyfication Apr 15 '19 at 23:12
  • I marked this as a duplicate, but please let me know if the duplicate does not resolve your question (and update your question with the specifics of what else you want). – Rufus L Apr 15 '19 at 23:19
  • The trick is to do something like `var output = p.StandardOutput.ReadToEnd();` before calling `process.WaitForExit();` – Rufus L Apr 15 '19 at 23:20
  • @RufusL - that question does not appear to answer my question. They are asking about reading a process's output in real-time rather than having to wait for the process to end, like the code in my question already does. I am asking about reading the process's output while not removing it from the console window. – Toby Smith Apr 15 '19 at 23:26
  • @Technoguyfication - I would like a standard console window to be shown. It's OK if it a replicated one - do you know how I can replicate a console window running a .bat file? – Toby Smith Apr 15 '19 at 23:29
  • @TobySmith You can either create a new project of type "Console application" that accepts data through a pipe and put it on the screen, or you you can try something along the lines of this question: https://stackoverflow.com/questions/4362111/how-do-i-show-a-console-output-window-in-a-forms-application – Technoguyfication Apr 15 '19 at 23:34
  • Follow up to that question.. do you need to send user input to the program? – Technoguyfication Apr 15 '19 at 23:34
  • @Technoguyfication Yes I do – Toby Smith Apr 15 '19 at 23:34
  • You can either send data through the STDIN, or, if you absolutely need it to look like a console, you could create your own console window that attaches to your program and displays the output and accepts the input, while allowing your existing code to do whatever it needs to do with the redirected STDIN and STDOUT. However, if you're using Winforms, I'd highly suggest just making a couple TextBoxes and calling it a day. – Technoguyfication Apr 15 '19 at 23:37
  • @RufusL - Unless I'm missing something, I have copied the top answer from that question and it does not do as you are describing. It is redirecting the standard output so that it no longer appears in the console window, which is not what I want. – Toby Smith Apr 16 '19 at 00:10
  • 1
    There is no "splitter" for streams. If you want one, you'll have to create one. For example, you could add calls to `Console.Error.Write` and `Console.Write` to `Process_ErrortDataReceived`. and `Process_OutputDataReceived. – Raymond Chen Apr 16 '19 at 00:30

0 Answers0