| 1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- 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<string> FindComPortNames(String VID, String PID)
- {
- String pattern = String.Format("^VID_{0}.PID_{1}", VID, PID);
- Regex _rx = new Regex(pattern, RegexOptions.IgnoreCase);
- List<string> comports = new List<string>();
- 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;
- }
- }
- }
|