C# 从进程读取标准输出,始终为空

C# 从进程读取标准输出,始终为空,c#,C#,运行此代码: Process p = new Process(); p.StartInfo.UseShellExecute = false; p.StartInfo.FileName = "tool.exe"; p.Start(); p.WaitForExit(); 使tool.exe运行,并在标准输出上输出一些内容。但是,如果我尝试使用以下方法捕获内容: Process p = new Process(); p.StartInfo.UseShellExecute = false; p.Sta

运行此代码:

Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = "tool.exe";
p.Start();
p.WaitForExit();
使tool.exe运行,并在标准输出上输出一些内容。但是,如果我尝试使用以下方法捕获内容:

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();
然后什么也不输出,即我的变量输出总是空的

我已经验证了tool.exe确实输出到标准输出,而不是标准错误

有人知道发生了什么事吗?在这里开始觉得自己很愚蠢,因为这似乎是一个真实的教科书示例

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

尝试以下操作:

它可能正在等待输入。感谢您的建议,但是如果运行tool.exe时没有参数,它会立即输出一些我试图捕获的内容。谢谢!但是还是没有运气。。没有任何东西通过。tool.exe是控制台应用程序吗?您是否尝试在cmd.exe中运行它?输出是什么?是的,这是我在项目中包含的一个控制台应用程序。它会打印几个字符串。请尝试对其他控制台应用程序执行相同的操作。带有ping.exe的4ex。它有用吗?