通过ManagementObjectSearcher读取串口信息可以使用以下步骤:
using System.Management;
string query = "SELECT * FROM Win32_SerialPort";
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
ManagementObjectCollection ports = searcher.Get();
foreach (ManagementObject port in ports)
{
string name = port["Name"].ToString();
string description = port["Description"].ToString();
string deviceId = port["DeviceID"].ToString();
// 处理串口信息...
}
注意:在使用ManagementObjectSearcher之前,需要确保你的应用程序有足够的权限访问WMI信息。