private System.IO.Ports.SerialPort com;
private void Form1_Load(object sender, EventArgs e)
{
com = new System.IO.Ports.SerialPort();
//获取所有的串口
Microsoft.VisualBasic.Devices.Computer pc = new Microsoft.VisualBasic.Devices.Computer();
foreach (string s in pc.Ports.SerialPortNames)
{
this.cbxPortName.Items.Add(s);
}
}
也可以
RegistryKey hklm = Registry.LocalMachine;
RegistryKey HARDWARE = hklm.OpenSubKey("HARDWARE");
RegistryKey DEVICEMAP = HARDWARE.OpenSubKey("DEVICEMAP");
RegistryKey SERIALCOMM = DEVICEMAP.OpenSubKey("SERIALCOMM");
string[] s = SERIALCOMM.GetValueNames();