gpt4 book ai didi

c# - 捕获的输出为空

转载 作者:行者123 更新时间:2023-11-30 13:58:34 24 4
gpt4 key购买 nike

我正在 try catch 其他应用程序的输出。捕获 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();

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

    p.StartInfo.FileName = "expdp";
p.StartInfo.Arguments = "help=y";

我错过了什么吗?

最佳答案

尝试检查 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();

有一点需要注意,如果您的输出流或错误流太长,那么这种方法可能会导致死锁。

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

关于c# - 捕获的输出为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16470394/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com