输出数据未满

时间:2014-10-20 09:07:06

标签: c# .net process

我需要一些帮助。我有一个外部应用程序(带有一些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);
    }

1 个答案:

答案 0 :(得分:0)

如果没有一个简洁但完整的代码示例可以可靠地证明问题,那么很难肯定。但是,如果您尝试在之后使用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在进程退出之前不会关闭)。