c# console redirection fails to work

Piotr Rybak 21 Reputation points
2021-04-07T10:31:50.697+00:00

I have code like below

var psi = new ProcessStartInfo
                {
                    FileName = "ffmpeg.exe",
                    Arguments = "-i " + fName + " -s " + Width + "x" + Height + " " + fName.Substring(0, fName.Length - 4) + "_.mp4",
                    RedirectStandardError = true,
                    RedirectStandardOutput = true,
                    RedirectStandardInput = true,
                    UseShellExecute = false
                };
                var p = Process.Start(psi);
                p.BeginErrorReadLine();
                p.BeginOutputReadLine();
                string fNamestderr = fName.Substring(0, fName.Length - 4) + "(stderr).txt";
                string fNamestdout = fName.Substring(0, fName.Length - 4) + "(stdout).txt";
                StreamWriter stdin = p.StandardInput;
                //stdin.WriteLine(["Command Text"]);
                p.OutputDataReceived += (s, evt) => {
                    if (evt.Data != null)
                    {
                        using (StreamWriter sw = File.AppendText(fNamestdout))
                        {
                            sw.WriteLine(evt.Data);
                        }
                        Console.WriteLine(evt.Data);
                    }
                };
                p.ErrorDataReceived += (s, evt) => {
                    if (evt.Data != null)
                    {
                        using (StreamWriter sw = File.AppendText(fNamestderr))
                        {
                            sw.WriteLine(evt.Data);
                        }
                        Console.WriteLine(evt.Data);
                    }
                };

I am trying to read console and see it at the sime time on screen in console.
I do

 p.BeginErrorReadLine();
    p.BeginOutputReadLine();

to start reading console but it fails to appear on screen so I am doing

Console.WriteLine(evt.Data);

I still cannot see anything on console.

AND SECOND ISSUE IS THAT console throws error that file already exists and will be overwritten
BUT it does not seem to be redirected so I am not getting any notice of it.

1) How to do both reading console output and seeing it on console at the same time?
2) What about overriding file error? Why am I not getting any notice of it? How to get it?

.NET CLI
.NET CLI
A cross-platform toolchain for developing, building, running, and publishing .NET applications.
322 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,238 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,036 Reputation points
    2021-04-07T11:12:19.4+00:00

    In regards to the second issue, assert if the file exists delete it. Here fileName is a variable containing the path and file name.

    if (File.Exists(fileName))
    {
        File.Delete(fileName);
    }
    

    Regarding the first issue, have you considered using a Windows Form project rather than a console project? If interested, I have a code samples.


  2. Viorel 112.1K Reputation points
    2021-04-07T11:28:01.923+00:00

    Try setting the event handler before starting the process. Therefore use another approach: create the process using ‘p = new Process()’, adjust p.StartInfo, set handlers, call p.Start(), etc.

    Also execute p.ExitProcess to wait for termination and to check the console and files.


  3. WayneAKing 4,921 Reputation points
    2021-04-07T22:02:53.257+00:00

    It sounds like you are looking for something like the "tee"
    command in Unix/Linux, which has been implemented in C/C++
    for DOS/Windows as well. See if this thread gives you any
    ideas for a possible approach:

    Mirroring console output to a file
    https://stackoverflow.com/questions/420429/mirroring-console-output-to-a-file

    • Wayne

  4. WayneAKing 4,921 Reputation points
    2021-04-08T16:38:52.643+00:00

    Problem is only when process is asking for key press.
    I am not getting final message that is asking to type
    something.

    Before asking for a key press, have you tried flushing
    the output stream buffer via something like this:

    Console.Out.Flush();
    
    • Wayne