using System.Management; //添加命名空间
private void GetComList()
{
try
{
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_PnPEntity"))
{
var hardInfos = searcher.Get();
int index = 1;
foreach (var hardInfo in hardInfos)
{
if (hardInfo.Properties["Name"].Value != null && hardInfo.Properties["Name"].Value.ToString().Contains("(COM"))
{
String strComName = hardInfo.Properties["Name"].Value.ToString();
comboBox1.Items.Add(strComName); //串口名称:strComName
index += 1;
}
}
}
}
catch
{
}
}
参考:博客园