通過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信息。