question

PiotrRybak-7170 avatar image
0 Votes"
PiotrRybak-7170 asked WayneAKing-0228 commented

c# console redirection fails to work

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?





dotnet-csharpdotnet-cli
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered karenpayneoregon commented

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

In second issue,I meant that I run process in console and do not know whether
it is trying to overwrite file till it shows up on screen. I must be able to intercept that message
BUT it fails to appear in redirected error neither in redirected output. How to get that message?

0 Votes 0 ·

Perhaps the following example can help.

https://gist.github.com/rivy/030df34899e4305ea5837586bf1ff7a4

0 Votes 0 ·
Viorel-1 avatar image
0 Votes"
Viorel-1 answered PiotrRybak-7170 commented

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.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

It is not terminating. It is waiting for keypress.

0 Votes 0 ·
WayneAKing-0228 avatar image
0 Votes"
WayneAKing-0228 answered joshbauer-3303 commented

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


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I tried
p.StandardError = new StreamReader() { };
It would not work as p.StandardError is only to read not to be set.
Cannot assign other value to it.

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

It is not showing up neither in StandardError,StandardOutput neither in StandardInput.

Normally prompt message is printed on console but after redirecting it is not there.
What is happening to message asking me to type something?
Where is it?


0 Votes 0 ·
WayneAKing-0228 avatar image
0 Votes"
WayneAKing-0228 answered WayneAKing-0228 commented

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

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

But it is process (not my program) that shows up prompting message.
When I redirect that process input and output, then thay message fails to be redirected to me.

What is happening to that message?
Where is that message when i am redirecting?
Why am i not getting that message anywhere?

0 Votes 0 ·

The console is attached to your process. So Flush
it after each Console.Writeline or Write.

  • Wayne

0 Votes 0 ·