控制台重定向输出到另一个应用程序 [英] Redirecting console output to another application
问题描述
这么说我运行我的程序:
So say I run my program:
Process proc = new Process();
proc.StartInfo.FileName = Path.GetDirectoryName(Application.ExecutablePath)
+ @"\Console.exe";
proc.Start();
和则希望输出我的控制台流,这个应用程序,我怎么会去这样做呢?
所以说我有:
And then wish to output my console stream to this application, how would I go about doing so? So say I have:
Console.WriteLine("HEY!");
我希望这样的节目中露面,我跑的控制台。我知道我必须用
I want that to show up in program that I ran's console. I know I have to redirect the output using
Console.SetOut(TextWriter);
但我不知道我怎么会去制作它写入其他程序。
But I have no idea how I would go about making it write to the other program.
我可以看到我怎么能做到这一点,如果我正在运行从Console.exe使用RedirectStandardInput我的主程序..但是这并不能真正帮助:p
I can see how I could do it if I were running my main program from Console.exe using RedirectStandardInput.. but that doesn't really help :P
推荐答案
RedirectStandardInput
使得Console.exe需要从中你可以在主访问流的输入程序。您可以直接写信给该流,或使用放样到那里重定向控制台输出...
RedirectStandardInput
makes Console.exe take its input from a stream which you can access in the main program. You can either write directly to that stream, or use SetOut to redirect console output there...
Process proc = new Process();
proc.StartInfo.FileName = Path.GetDirectoryName(Application.ExecutablePath)
+ @"\Console.exe";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardInput = true;
proc.Start();
proc.StandardInput.WriteLine("Hello");
Console.SetOut(proc.StandardInput);
Console.WriteLine("World");
修改
这有可能是Console.exe不具有管道输送到它,而不是交互输入数据处理好。你可以在命令行的东西像
It's possible that Console.exe doesn't cope well with having data piped into it rather than entered interactively. You could check this from the command line with something like
echo "Hello" | Console.exe
如果不这样做你所期望的,您重定向程序的输出也不会有的。要测试你的程序,而无需担心目标程序,你可以尝试
If that doesn't do what you expect, redirecting your program's output won't either. To test your program without worrying about the target program, you could try
proc.StartInfo.FileName = @"cmd";
proc.StartInfo.Arguments = @"/C ""more""";
如果显示,你写它的文字,那么问题是在接收端。
If that displays the text that you write to it, then the problem is on the receiving end.
这篇关于控制台重定向输出到另一个应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!