WMI not working after upgrading to Windows 10

后端 未结 3 554
旧时难觅i
旧时难觅i 2021-01-13 21:02

I had been using the code below for a console application in Visual Studio on Windows 8 to return the description and device ID of connected serial devices. I was using a mo

3条回答
  •  离开以前
    2021-01-13 21:54

    A bit smaller solution than Juderb

        public static List FindComPorts()
        {
            using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity WHERE ConfigManagerErrorCode = 0"))
            {
                return searcher.Get().OfType()
                    .Select(port => Regex.Match(port["Caption"].ToString(), @"\((COM\d*)\)"))
                    .Where(match => match.Groups.Count >= 2)
                    .Select(match => match.Groups[1].Value).ToList();
            }
        }
    

    Tested this on win7 + win10. Btw, you can add extra search criteria to the regex if you want (or just add a new Where clause)

提交回复
热议问题