using Microsoft.Win32; using System; using System.Collections.Generic; using System.IO.Ports; using System.Linq; using System.Text.RegularExpressions; namespace DeviceCenter { class PortUtil { public static List FindComPortNames(String VID, String PID) { String pattern = String.Format("^VID_{0}.PID_{1}", VID, PID); Regex _rx = new Regex(pattern, RegexOptions.IgnoreCase); List comports = new List(); RegistryKey rk1 = Registry.LocalMachine; RegistryKey rk2 = rk1.OpenSubKey("SYSTEM\\CurrentControlSet\\Enum"); foreach (String s3 in rk2.GetSubKeyNames()) { RegistryKey rk3 = rk2.OpenSubKey(s3); foreach (String s in rk3.GetSubKeyNames()) { if (_rx.Match(s).Success) { RegistryKey rk4 = rk3.OpenSubKey(s); foreach (String s2 in rk4.GetSubKeyNames()) { RegistryKey rk5 = rk4.OpenSubKey(s2); string location = (string)rk5.GetValue("LocationInformation"); RegistryKey rk6 = rk5.OpenSubKey("Device Parameters"); string portName = (string)rk6.GetValue("PortName"); if (!String.IsNullOrEmpty(portName) && SerialPort.GetPortNames().Contains(portName)) comports.Add((string)rk6.GetValue("PortName")); } } } } return comports; } } }