我需要一个与我的 C# .NET Windows 桌面应用程序的某个 VID/PID 匹配的已连接 WinUsb 设备的 GUID 列表。我尝试从此处所述的所有已连接 USB 设备的列表开始。
我得到以下运行时异常:
System.Management.ManagementException: 'Invalid class '
This exception was originally thrown at this call stack:
System.Management.ManagementException.ThrowWithExtendedInfo(System.Management.ManagementStatus)
System.Management.ManagementObjectCollection.ManagementObjectEnumerator.MoveNext()
这是我的(简化的)代码:
private static List<Guid> GetWinUsbDevices()
{
List<Guid> deviceList = new List<Guid>();
using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBControllerDevice"))
using (ManagementObjectCollection collection = searcher.Get())
foreach (var device in collection)
{
// Check device VID/PID
// Read device GUID
deviceList.Add(new Guid());
}
return deviceList;
}
foreach
循环开始时抛出异常(特别是当它命中时in
)。
我也试过ManagementClass.GetInstances()
了,结果相似。