如何读取我自己的应用程序的标准输出 [英] How to read standard output of my own application
问题描述
我有一个应用程序,必须读取通过
编写的自己的输出Console.WriteLine("blah blah");
我正在尝试
Process p = Process.GetCurrentProcess();
StreamReader input = p.StandardOutput;
input.ReadLine();
但是由于第二行的"InvalidOperationException"而无法使用.上面写着"StandardOutput尚未重定向,或者进程尚未开始"之类的东西(翻译)
我如何读取自己的输出?还有另一种方法吗?并完成如何编写我自己的输入?
带有输出的应用程序已经在运行.
我想读取它的输出在同一应用程序中居住.没有第二个应用程序.只有一个.
我只是想知道您的意图是什么,但是如果您想从启动的应用程序中读取输出,则可以重定向输出.
// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "Write500Lines.exe";
p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected stream.
// p.WaitForExit();
// Read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
来自 http://msdn.microsoft的 示例. com/en-us/library/system.diagnostics.process.standardoutput.aspx
如果您要按照编辑指定的方向重定向当前控制台应用程序的输出,则可以使用.
private static void Main(string[] args)
{
StringWriter writer = new StringWriter();
Console.SetOut(writer);
Console.WriteLine("hello world");
StringReader reader = new StringReader(writer.ToString());
string str = reader.ReadToEnd();
}
I have an application that must read it's own output that is written via
Console.WriteLine("blah blah");
I'm trying
Process p = Process.GetCurrentProcess();
StreamReader input = p.StandardOutput;
input.ReadLine();
But it doesn't work because of "InvalidOperationException" at the second line. It says something like "StandardOutput wasn't redirected, or the process has not been started yet" (translated)
How can I read my own output ? Is there another way to do that ? And to be complete how to write my own input ?
The application with the output is running already.
I want to read it's output live in the same application. There is no 2nd app. Only one.
I'm just guessing as to what your intention might be but if you want to read the output from a application you started you can redirect the output.
// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "Write500Lines.exe";
p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected stream.
// p.WaitForExit();
// Read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
example from http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx
Edit:
If you want to redirect the output of your current console application as your edit specifies you can use.
private static void Main(string[] args)
{
StringWriter writer = new StringWriter();
Console.SetOut(writer);
Console.WriteLine("hello world");
StringReader reader = new StringReader(writer.ToString());
string str = reader.ReadToEnd();
}
这篇关于如何读取我自己的应用程序的标准输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!