通过关键字,自动筛选指定串口名称

ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.FileName = "C:\\Windows\\System32\\wbem\\WMIC.exe";
            startInfo.Arguments = "PATH Win32_SerialPort  GET /VALUE";
            startInfo.RedirectStandardOutput = true;
            startInfo.UseShellExecute = false;
            startInfo.CreateNoWindow = true;

            Process processTemp = new Process();
            processTemp.StartInfo = startInfo;
            processTemp.EnableRaisingEvents = true;

            processTemp.Start();
            string output = processTemp.StandardOutput.ReadToEnd();
            int indexOfBGEntry = output.IndexOf("");  //input filter keywords
            if (indexOfBGEntry > -1)
            {
                string output_sub = output.Substring(indexOfBGEntry);
                string str = "DeviceID=";
                int i = output_sub.IndexOf(str);
                int start = i + str.Length;

                string substr = output_sub.Substring(start);
                int end = substr.IndexOf("\r");

                return output_sub.Substring(start, end);
            }
            return null;

 

posted @ 2017-08-30 09:13  cnblogs1_1  阅读(245)  评论(0编辑  收藏  举报