繁体   English   中英

C#检测usb设备ClassCode(usb设备类型)

[英]C# detect usb device ClassCode (usb device type)

我需要知道系统中目前使用的是什么类型的USB设备。 关于USB设备的类代码有USB规范 但是我无法获得设备类型,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
        }
}

您可以下载USB View Source作为起点。 这将循环通过PC上的所有USB设备(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"));
   // ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM