Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read last line of STDOUT

Tags:

c#

.net

stdout

I am executing a program through Process.Start(). I tried redirecting the output to get the lines.

For now, however, I only need the last line.

Is there a way to read the last line only or do I still need to the whole redirectStandaroutput = false; etc?

Is there a simpler way?

If I have to go by the process.Redirect(), I tried following the examples on MSDN and other sources with outputdatareceived event, but my output only gets written to the console at the end and is not async.

like image 494
user393148 Avatar asked Nov 13 '22 07:11

user393148


1 Answers

Once you use process.Start() the only way to read output that I know is to use .RedirectStandardInput = true;. And as Standart output is a stream object, last line could be read only by reading whole stream.

BTW proc.StandardOutput.ReadToEnd() will hang your thread untill process will exit, and if you'll get any promts during reading - your app will hang up.

like image 138
Johnny_D Avatar answered Nov 16 '22 02:11

Johnny_D