充分利用一个可执行输出的另一 [英] Getting output from one executable in an other one

查看:193
本文介绍了充分利用一个可执行输出的另一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在试图让一个可执行的控制台应用程序的输出到另一个。准确的说,是什么我试图做一些概述:

I'm currently trying to get the output of an executable console-app into an other one. To be exact, a little overview of what I'm trying to do:

我有一个可执行文件,我不能编辑和既看不到它的代码。它写入一些(一大群说实话)线到执行时的控制台。

I have one executable which I cannot edit and neither see it's code. It writes some (quite a bunch to be honest) lines into the console when executed.

现在我想写启动上面的一个,读的东西另一个可执行程序,它写道。

Now I want to write another executable that starts the one above and reads the things it writes.

看起来很简单我,所以我就开始编码,但结束了一个错误信息说 StandardOut没有被重定向或过程不是招尚未开始

Seems simple to me, so I started coding but ended up with an error message saying that StandardOut has not been redirected or the process hasn't started yet.

我试了一下使用这个还挺结构(C#):

I tried it using this kinda structure (C#):

Process MyApp = Process.Start(@"C:\some\dirs\foo.exe", "someargs");
MyApp.Start();
StreamReader _Out = MyApp.StandardOutput;

string _Line = "";

while ((_Line = _Out.ReadLine()) != null)
    Console.WriteLine("Read: " + _Line);

MyApp.Close();



我可以打开可执行,它也确实打开内的人,但只要它涉及到读返回值,应用程序崩溃。

I can open the executable and it also does open the one inside, but as soon as it comes to reading the returned values, the app crashes.

我在做什么错?!

推荐答案

在对的 Process.StandardOutput 财产。从文档您将需要设置一个布尔值,指示要重定向以及禁用外壳程序执行流

Take a look at the documentation for the Process.StandardOutput property. You will need to set a boolean indicating that you want the stream redirected as well as disabling shell execute.

请注意:

要使用StandardOutput,必须的ProcessStartInfo .. ::。UseShellExecute设置为false,并且必须设置的ProcessStartInfo .. ::。RedirectStandardOutput为true。否则,从StandardOutput流读取抛出一个异常

To use StandardOutput, you must set ProcessStartInfo..::.UseShellExecute to false, and you must set ProcessStartInfo..::.RedirectStandardOutput to true. Otherwise, reading from the StandardOutput stream throws an exception

您需要改变你的代码一点点调整的变化:

You would need to change your code a little bit to adjust for the changes:

Process myApp = new Process(@"C:\some\dirs\foo.exe", "someargs");
myApp.StartInfo.UseShellExecute = false;
myApp.StartInfo.RedirectStandardOutput = false;

myApp.Start();

string output = myApp.StandardOutput.ReadToEnd();
p.WaitForExit();

这篇关于充分利用一个可执行输出的另一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆