接收延迟的标准输出消息

时间:2015-10-10 13:10:01

标签: c#

我试图在我的应用程序中读取以下devcon.exe命令的标准输出:

devcon rescan 

如果我在cmd中运行它,我会得到以下两行:

Scanning for new hardware.
Scanning completed.

第一行立即打印,而第二行打印时延迟很短(这几乎肯定是导致问题的原因)。

我想在我的应用程序中获得此输出。我有以下方法可以做到这一点,但只是打印,作为接收输出,第一行(输出存储在'消息'变量)。我得到的命令的return code是2,这意味着它无法正确执行:

public Boolean rescanProgrammer()
    {
        Boolean rescanCompleted = false;
        String path_to_devcon = System.Environment.CurrentDirectory.ToString() + "\\devcon.exe";
        Boolean oneResultFound = false;

        if (File.Exists(path_to_devcon))
        {
            Process externalApp = new Process();
            externalApp.StartInfo.Verb = "runas"; // run as admin (only for vista and up)
            externalApp.StartInfo.FileName = path_to_devcon;
            externalApp.StartInfo.Arguments = "rescan"; 
            externalApp.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            externalApp.StartInfo.UseShellExecute = false;
            externalApp.StartInfo.RedirectStandardOutput = true;
            externalApp.Start();

            List<String> message = new List<String>();

            using (StreamReader reader = externalApp.StandardOutput)
            {
                while (!reader.EndOfStream)
                {
                    string result = reader.ReadLine();
                    message.Add(result);
                    // Check for the line : "1 matching device(s) found."
                    // Usually it's the last line in the output

                    if (result.Contains("Scanning completed."))
                    {
                        oneResultFound = true;
                        break;
                    }
                }
            }

            externalApp.WaitForExit();
            // Returned Devcon.exe exit code
            /*
             Return code    Meaning
                0           Success
                1           Requires reboot
                2           Failure
                3           Syntax error
            */
            MessageBox.Show(externalApp.ExitCode + "");

            // Message
            MessageBox.Show(String.Join("\n", message.ToArray()));

            //Check if the line was found
            if (oneResultFound == true)
            {
                // We found the above line and the process exited, so we can
                // safely return a value
                rescanCompleted = true;
                MessageBox.Show("\"Rescan completed\" line found!");
            }
            else
            {
                MessageBox.Show("Rescan operation didn't complete successfully!");
            }
        }
        else
        {
            MessageBox.Show("Couldn't find Devcon.exe");
        }
        return rescanCompleted;
    }

有人知道,为了获得全部输出,我需要更改什么?

我也尝试使用StreamReader.ReadToEnd(),但也没有用:

using (StreamReader reader = externalApp.StandardOutput)
{
    String temp = reader.ReadToEnd();
    MessageBox.Show(temp);
}

有人知道如何解决这个问题吗?提前谢谢。

0 个答案:

没有答案
相关问题