Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
193 views
in Technique[技术] by (71.8m points)

c# - Need an aclaration about the console buffer and console outputs

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

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

1 Reply

0 votes
by (71.8m points)

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#


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...