获取设备管理器端口号
static void Main(string[] args) { List<string> vaildportname = new List<string>(); foreach (string portname in SerialPort.GetPortNames()) { bool vaild = true; SerialPort vaildport = new SerialPort(portname); try { vaildport.Open(); } catch { vaild = false; } finally { vaildport.Close(); } if (vaild == true) { vaildportname.Add(portname); } } foreach (string s in vaildportname) { Console.WriteLine(s); } Console.ReadKey(); }