【问题标题】:C# detect usb device ClassCode (usb device type)C#检测usb设备ClassCode(usb设备类型)
【发布时间】:2013-08-15 12:10:40
【问题描述】:

我需要知道系统中当前使用的 USB 设备类型。有一个关于 USB 设备类代码的USB specification。但我无法获取设备类型,WMI 请求WQL: select * from Win32_UsbHub 在类代码、子类代码、协议类型字段上给出空值。任何想法如何检测当前使用的 USB 设备类型?

我当前的代码:

ManagementObjectCollection collection; 
using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub")) 
{
    collection = searcher.Get();
    foreach (var device in collection)
        {
            var deviceId = (string)GetPropertyValue("DeviceID");
            var pnpDeviceId = (string)GetPropertyValue("PNPDeviceID");
            var descr = (string)device.GetPropertyValue("Description");
            var classCode = device.GetPropertyValue("ClassCode"); //null here
        }
}

【问题讨论】:

  • 您能否发布完整的示例,包括ManagementScopeObjectQueryManagementObjectSearcher 的用法?
  • ManagementObjectCollection 集合;使用 (var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub")) 集合 = searcher.Get();
  • wbemtest.exe 工具效果相同:Class cod, Subclass code, Protocol type 字段为空
  • 如您链接的 USB 规范中所述,您必须深入研究 device descriptor(设备类)或接口描述符(接口类)才能检索该信息。单独使用 WMI 可能无法做到这一点。
  • 您在寻找什么类型的设备?如果您正在寻找特定的设备类型(如虚拟 Com 端口),您最好查看专门针对这些项目的 wmi 查询。

标签: c# usb wmi


【解决方案1】:

您可以下载USB View Source 作为起点。这将遍历 PC (C#) 上的所有 USB 设备并提取有关每个设备的信息。要获取Class codeSubclass codeProtocol 类型字段,您需要稍微修改它。更改下面的内容并运行它,您将通过单击树视图中的项目来获取每个 USB 设备的信息(信息将显示在右侧面板中)。

对 USB.cs 的修改:

// Add the following properties to the USBDevice class
// Leave everything else as is
public byte DeviceClass
{
   get { return DeviceDescriptor.bDeviceClass; }
}

public byte DeviceSubClass
{
   get { return DeviceDescriptor.bDeviceSubClass; }
}

public byte DeviceProtocol
{
   get { return DeviceDescriptor.bDeviceProtocol; }
}

对 fmMain.cs 的修改

// Add the following lines inside the ProcessHub function
// inside the "if (port.IsDeviceConnected)" statement
// Leave everything else as is
if (port.IsDeviceConnected)
{
   // ...
   sb.AppendLine("SerialNumber=" + device.SerialNumber);
   // Add these three lines
   sb.AppendLine("DeviceClass=0x" + device.DeviceClass.ToString("X"));
   sb.AppendLine("DeviceSubClass=0x" + device.DeviceSubClass.ToString("X"));
   sb.AppendLine("DeviceProtocol=0x" + device.DeviceProtocol.ToString("X"));
   // ...
}

【讨论】:

  • 看来是我需要的,我试试看。
  • @PeterJ - 看起来文件已经重组,我已经更新了链接。
猜你喜欢
  • 2012-05-10
  • 1970-01-01
  • 2011-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多