本文介绍了需要关于控制台缓冲区和控制台输出的声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用API​​技术在.NET中使用ReadConsoleOutput函数检索控制台缓冲区时,这意味着我在同一个输出(缓冲区)中可以拥有标准输出,输入输出和错误输出? / p>

或换句话说:



如果我们通常需要启动一个进程并通过分离重定向StandrdOutput / ErrorOutput / InputOutput输出总共3个输出,那么如果我们使用一种技术读取控制台缓冲区而不是重定向3个输出,我们可以将这3个输出放入缓冲区strean中的all-in-one输出中。



我问了一个关于如何读取控制台缓冲区的问题(这里:),因为我想象,缓冲区将包含所有这些输出合并在一起,所以我可以检索所有的standardOutput或ErrorOutput或InputOutput到缓冲区流而不需要通过分隔的方式重定向ANY任何输出。



控制台缓冲区包含所有输出合并(所有的事情,写入控制台,没有例外)或只有一个输出?



所以,我想知道如果我错了或不是我说的话。

更好的例子可能是,如果我有这个过程, t重定向任何输出:

 私有共享进程_Without_Redirect作为新进程()使用{_ 
.StartInfo = New ProcessStartInfo With {_
.CreateNoWindow = True,_
.UseShellExecute = False,_
.RedirectStandardInput = False,_
.RedirectStandardError = False,_
.RedirectStandardOutput = False _
}}

...进程是一个CLI进程,如果我使用技术来读取启动过程的控制台缓冲区,我将能够从缓冲区检索标准和错误输出?缓冲区将存储进程的所有输出?



启动的进程写入控制台的所有内容都将存储到缓冲区中?所以我可以检索缓冲区的所有在一个输出,而不是重定向和读取每个过程输出?

解决方案

请使用 Console.SetOut Console.Out 将缓冲区重定向到流。
那么你可以做任何你需要的。



  Console.WriteLine 你好,世界); 
FileStream fs = new FileStream(Test.txt,FileMode.Create);
//首先,保存标准输出。
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();

另一个例子。如何将控制台输出重定向到文本框:




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.

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?

解决方案

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#

这篇关于需要关于控制台缓冲区和控制台输出的声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 22:43