捕获的输出为空

use*_*126 3 c#

我正在尝试捕获其他应用程序的输出.捕获ping的输出效果很好.变量输出包含预期输出.

    var p = new Process();
    p.StartInfo.FileName = "ping";
    p.StartInfo.Arguments = "www.google.com";
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.CreateNoWindow = true;
    p.Start();

    var output = p.StandardOutput.ReadToEnd();
    p.WaitForExit();
Run Code Online (Sandbox Code Playgroud)

但是,当我使用此代码捕获expdp的输出(这是导出的oracle工具)时,该变量为空.在控制台中运行相同的命令将返回一些输出.

    p.StartInfo.FileName = "expdp";
    p.StartInfo.Arguments = "help=y";
Run Code Online (Sandbox Code Playgroud)

我错过了什么吗?

Sam*_*ica 7

尝试检查StandardError流,看看你是否得到任何东西

var p = new Process();
p.StartInfo.FileName = "expdp";
p.StartInfo.Arguments = "help=y";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();

var error = p.StandardError.ReadToEnd();
var output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Run Code Online (Sandbox Code Playgroud)

有一点需要注意,如果您的输出流或错误stram太长,则此方法可能会导致死锁.

如果是这种情况,您将不得不异步读取其中一个流.