下面的代码使用了GetPortNames,获取了串口列表,
MyBox.Items.Add(i);
这个语句将串口的清单添加到列表中去
最后使用语句将最后一个,来显示到combox中去
//刷新com
private string SearchAndAddSerialToComboBox(SerialPort MyPort, ComboBox MyBox)
{
//将可用端口号添加到ComboBox
string[] ports = System.IO.Ports.SerialPort.GetPortNames();//获取电脑上可用串口号
MyBox.Items.Clear(); //清空ComboBox内容
foreach(string i in ports)
{
MyBox.Items.Add(i);
}
MyBox.Text = ports[0]; //初始化
return ports[0];
}