//方法一:
using Microsoft.Win32;
// 获取所有串口名,夹杂在ComboBox中
public void GetComList(ComboBox cmbComPort)
{
RegistryKey keyCom = Registry.LocalMachine.OpenSubKey("Hardware\\DeviceMap\\SerialComm");
//(*string []RegistryKey.GetValueNames()检索包含于此项关联的所有值名称的字符串数组)
if (keyCom != null)
{
string[] sSubKeys = keyCom.GetValueNames();
cmbComPort.Items.Clear();
foreach (string sName in sSubKeys)
{
string sValue = (string)keyCom.GetValue(sName);
cmbComPort.Items.Add(sValue);
}
}
}
方法二:获取串口名称加载在TextBox中
using System.IO.Ports;
foreach (string vPortName in SerialPort.GetPortNames())
{
this.textBox2.Text = this.textBox2.Text + vPortName + " ";
}