我试着像:
foreach (ManagementObject queryObj in searcher.Get())
{
Console.WriteLine("PortName: {0}", queryObj["PortName"]);
}
它在控制台
中为我提供了not found
答案 0 :(得分:1)
这应该可以解决问题,或者至少让你指向正确的方向:
ManagementObjectSearcher comPortSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity WHERE ConfigManagerErrorCode = 0");
foreach (ManagementObject service in comPortSearcher.Get())
{
Console.WriteLine(service.ToString());
}
实际上,您需要指定正确的查询。
答案 1 :(得分:0)
使用此查询
string[] portnames = SerialPort.GetPortNames();
foreach( string port in portnames)
{
Console.WriteLine(port);
}