简体   繁体   English

读取过程的标准输出,始终为空

[英]Reading standard output from process, always empty

Running this code: 运行此代码:

Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = "tool.exe";
p.Start();
p.WaitForExit();

Makes tool.exe run, and output some content on standard output. 使tool.exe运行,并在标准输出上输出一些内容。 However, if I try to capture the content using: 但是,如果我尝试使用以下方法捕获内容:

Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.FileName = "tool.exe";
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();

Console.WriteLine(output);
Console.ReadLine();

Then nothing gets outputted, ie my variable "output" is always empty. 然后没有任何输出,即我的变量“输出”始终为空。

I've verified that tool.exe indeed outputs to standard output (and not standard error). 我已经验证了tool.exe确实可以输出到标准输出(而不是标准错误)。

Anyone have a clue of what's going on? 任何人都知道发生了什么事吗? Starting to feel stupid here, as it seems to be a real text book example... 在这里开始感到愚蠢,因为这似乎是一个真实的教科书示例...

p.OutputDataReceived += new DataReceivedEventHandler
        (
            delegate(object sender, DataReceivedEventArgs e)
            {                   
                using (StreamReader output = p.StandardOutput)
                {
                    retMessage = output.ReadToEnd();
                }
            }
        );

Try this :) 尝试这个 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM