输出数据未满

本文关键字:数据 输出 | 更新日期: 2023-09-27 18:27:03

我需要一些帮助。我有一个外部应用程序(test.exe和一些dll文件)。在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);
    }

输出数据未满

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

也许可以试试这个:

        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在进程退出之前不会关闭)。