我正在使用c#FW 4.0。 我想迭代我的计算机中的所有串行端口并获取每个端口的全名。 例如,我想看到"多产USB到串口通讯端口(COM6)"而不只是COM6。
这是我目前的代码,它只给我COM / 1/6等......
string[] ports = SerialPort.GetPortNames();
foreach (string port1 in ports)
{
MessageBox.Show(port1);
}
答案 0 :(得分:2)
您可以使用WMI,查看WMI Reference
Juanma回答,Here。
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\WMI",
"SELECT * FROM MSSerial_PortName");
foreach (ManagementObject queryObj in searcher.Get())
{
Console.WriteLine("-----------------------------------");
Console.WriteLine("MSSerial_PortName instance");
Console.WriteLine("-----------------------------------");
Console.WriteLine("InstanceName: {0}", queryObj["InstanceName"]);
Console.WriteLine("-----------------------------------");
Console.WriteLine("MSSerial_PortName instance");
Console.WriteLine("-----------------------------------");
Console.WriteLine("PortName: {0}", queryObj["PortName"]);
//If the serial port's instance name contains USB
//it must be a USB to serial device
if (queryObj["InstanceName"].ToString().Contains("USB"))
{
Console.WriteLine(queryObj["PortName"] + "
is a USB to SERIAL adapter/converter");
}
}
}
catch (ManagementException e)
{
MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
}