Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

When using API techniques to retrieve the console buffer in .NET using the 'ReadConsoleOutput' function, that means what I can have in the same output (buffer) the Standard output, Input output, and also Error output all together?

Or in other words:

If we normally need to start a process and redirect by separated the StandrdOutput/ErrorOutput/InputOutput output into a total of 3 outputs, then if we use a technique to read the console buffer instead redirecting 3 outputs, we can have those 3 outputs into all-in-one output inside the buffer strean?

I asked a question about how to read the console buffer (here: How to read Console buffer in VBNET?) because I've imagined that the buffer would contains all those outputs 'merged' together so I could retrieve all of standardOutput or ErrorOutput or InputOutput into the buffer stream without needing to redirect ANY of the output by separated.

The console buffer contains all outputs merged (all the things which are written inc console, without exceptions) or just has one output?

So, what I want to know if I'm wrong or not with something that I've said.

UPDATE:

A better example maybe, if I have this process where I don't redirect any output:

Private Shared Process_Without_Redirect As New Process() With { _
.StartInfo = New ProcessStartInfo With { _
            .CreateNoWindow = True, _
            .UseShellExecute = False, _
            .RedirectStandardInput = False, _
            .RedirectStandardError = False, _
            .RedirectStandardOutput = False _
}}

...the process is a CLI process so if I use a technique to read the console buffer of the launched process, I would be able to retrieve Standard and Error outputs from the buffer? the buffer will store all outputs of the process?

all the things that the launched process writes to console will be stored into the buffer? so I can retrieve the buffer where all-in-one outputs are written instead redirecting and reading each process output?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
187 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can use Console.SetOut and Console.Out to redirect the buffer to a stream. then you can do whatever you need with it.

Console.SetOut Method

Console.WriteLine("Hello World");
FileStream fs = new FileStream("Test.txt", FileMode.Create);
// First, save the standard output.
TextWriter tmp = Console.Out;
StreamWriter sw = new StreamWriter(fs);
Console.SetOut(sw);
Console.WriteLine("Hello file");
Console.SetOut(tmp);
Console.WriteLine("Hello World");
sw.Close();

Another example. How to redirect the console output to a textbox:

How to redirect the Console’s output to a TextBox in C#


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...