Imagine that from my application I launch a Commandline application that prints on the standard output this line:
I'm a standard line with special character: #
And after that, the same application prints this line in the error output:
I'm an error line with special character: @
So the CMD output is exactly this regardless of whether the kind of output:
I'm a standard line with special character: #
I'm an error line with special character: @
Then, regardless of whether it is the standard or error output, How I could read both lines at the same time? (the full CMD output) , or in other words, how to read the console buffer?
CLARIFICATION:
I don't mean how to redirect a process output to read the standard input, or the error output, or both, I already know how to do that, what I mean is to read the console buffer, a buffer where (I think) all outputs (Standard/Error/Input) are merged as only one output in the stream with no needing to redirect the desired output, just reading the buffer stream I could search for a special character printed by the application, regardless the kind of output which printed the special character.
To understand it better, this is a pseudo-code:
Private Sub ReadConsoleBuffer()
Dim p As New Process With
{.StartInfo = New ProcessStartInfo With
{.FileName = "MyApp.exe",
.UseShellExecute = False,
.RedirectStandardError = False,
.RedirectStandardOutput = False}} ' There is no need to redirect output because I'm not trying to capture the outputs by myself.
p.Start()
' Process will write an "#" character on the Standard Output,
' And will write an "@" character on the Error Output.
Dim cBuffer As IO.StreamReader = ' ...Function to retrieve the console buffer...
Do Until cBuffer.EndOfStream
Select Case Convert.ToChar(cBuffer.Read)
Case "#"
' "#" character found so we succesfully read the Standard Output from the buffer.
Case "@"
' "@" character found so we succesfully read the Error Output from the buffer.
End Select
cBuffer = ' ...Reassign the buffer data to retrieve new printed lines by the process?...
' really I'm not sure that this would be necessary or not.
Loop
End Sub
I hope that you could notice right the difference between what I would like to do, and what I don't would like to do, the code above is what I would like to do, and this else example of a normally output redirection is what I don't would like to do:
Private Sub ReadConsoleBuffer() Handles MyBase.Shown
Dim p As New Process With
{.StartInfo = New ProcessStartInfo With
{.FileName = "cmd.exe",
.Arguments = "/C Dir /W *.ext not found",
.UseShellExecute = False,
.RedirectStandardError = True,
.RedirectStandardOutput = True}}
p.Start()
Do Until (p.StandardOutput.EndOfStream And p.StandardError.EndOfStream)
RTB_stdOut.AppendText(p.StandardOutput.ReadLine & Environment.NewLine)
RTB_errOut.AppendText(p.StandardError.ReadLine & Environment.NewLine)
Loop
End Sub
PS: This question is the second part of these else uncleared questions:
Need an aclaration about the console buffer and console outputs
How to read Console buffer in VBNET?
I've already know how to do those things but seems that people couldn't understand my problem, I wonder that this time I've exposed a better explanation of my problem and what I'm trying to do.