【问题标题】:output limitation when calling exe file from c# code从 C# 代码调用 exe 文件时的输出限制
【发布时间】:2019-06-25 06:38:21
【问题描述】:

我有一个 exe 文件,我使用 c# 中的 Process 命名空间从我的 c# 代码中调用它。
我使用 ReadToEnd() 方法捕获输出。 (在 exe 源代码中,我使用了打印方法来获得所需的输出)。
问题是当我的输出变大时,exe 文件的执行会崩溃并挂起。
调用exe函数时输出大小有限制吗?

Process p = new Process();
p.StartInfo.FileName = command;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.Arguments = args;
p.Start();
p.WaitForExit();
string output = p.StandardOutput.ReadToEnd();
return output;

【问题讨论】:

  • 首先,您需要实际阅读 documentation 以完成此特定任务。另一件事是我相信您应该尝试采用异步方法,也可以找到here
  • 你得到的错误/异常是什么?
  • 我没有收到任何错误消息。但是运行 exe 文件时的控制台页面会永远保留。

标签: c# exe std


【解决方案1】:

您遇到的是僵局。 documentation 中描述了这种情况。为了避免这种情况,请确保等待子进程退出之前阅读输出。

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

【讨论】:

    【解决方案2】:

    我记得有和你在这里描述的同样的问题,我当时做的是这样的:

    private string RunProcess(string command, string args)
    {
        StringBuilder sb = new StringBuilder();
        Process p = new Process();
        p.StartInfo.FileName = command;
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.CreateNoWindow = true;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.Arguments = args;
        p.OutputDataReceived += (sender, eventArgs) => { sb.Append(eventArgs.Data); };
        p.Start();
        p.WaitForExit();
        return sb.ToString();
    }
    

    【讨论】:

      猜你喜欢
      • 2014-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多