【问题标题】:Output data is not full输出数据未满
【发布时间】:2014-10-20 09:07:05
【问题描述】:

我需要一些帮助。我有一个外部应用程序(带有一些 dll 文件的 test.exe)。在 cmd 中,我运行如下命令:test.exe parmeters 并获取大量数据和一些需要的信息。

我编写了执行此外部应用程序的应用程序,并且输出不完全,因为我使用 cmd 执行它。这只是一些第一行。我不介意有什么问题。请帮忙

using(var process = new Process {
    StartInfo = new ProcessStartInfo {
        UseShellExecute = false,
        RedirectStandardOutput = true,
        RedirectStandardError = true,
        FileName = orPath,
        Arguments = parmeters.ToString(),
    }
}) {
    process.Start();
    process.WaitForExit();
    string result = "";
    string standard_output;
    while ((standard_output = process.StandardOutput.ReadLine()) != null) {
        if (standard_output.Contains("xx"))
            result = standard_output.Substring(standard_output.Length - 15);
    }

【问题讨论】:

  • 您的问题并不完全清楚,您能否尝试改写您的问题?什么输出不完全,你期待什么? argBuilder 中的内容是什么?

标签: c# .net process


【解决方案1】:

如果没有一个简洁但完整的代码示例来可靠地演示该问题,就很难确定。但如果您尝试在调用WaitForExit() 之后 使用StandardOutput,我并不感到惊讶,并不是所有的输出都已被缓冲并且可用。

不妨试试这个:

        process.Start();
        string result = "";
        string standard_output;
        while ((standard_output = process.StandardOutput.ReadLine())
                                != null)
        {
            if (standard_output.Contains("xx"))
                result = standard_output.Substring(
                       standard_output.Length - 15);
        }

请注意,我只是删除了对WaitForExit() 的调用。读取 StandardOutput TextReader 直到它返回 null 将具有与等待进程结束相同的效果,假设是一个正常进程(即在进程退出之前 stdout 不会关闭)。

【讨论】:

    猜你喜欢
    • 2021-01-24
    • 2020-07-10
    • 1970-01-01
    • 1970-01-01
    • 2020-12-18
    • 2020-03-10
    • 2014-06-23
    • 1970-01-01
    • 2019-03-15
    相关资源
    最近更新 更多