简体   繁体   English

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

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

I need to know what kind of USB devices currently used in system. 我需要知道系统中目前使用的是什么类型的USB设备。 There is a USB specification about class codes of USB devices. 关于USB设备的类代码有USB规范 But I cant get device type, WMI request WQL: select * from Win32_UsbHub give null values on Class code, Subclass code, Protocol type fields. 但是我无法获得设备类型,WMI请求WQL: select * from Win32_UsbHub在类代码,子类代码,协议类型字段上给出空值。 Any ideas how to detect USB device type currently in use? 有关如何检测当前使用的USB设备类型的任何想法?

My current code: 我目前的代码:

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
        }
}

You can download USB View Source as a starting point. 您可以下载USB View Source作为起点。 This loops through all USB devices on a PC (C#) and pulls information about each. 这将循环通过PC上的所有USB设备(C#)并提取有关每个USB设备的信息。 To get the Class code , Subclass code , and Protocol type fields, you'll need to modify it slightly. 要获取Class codeSubclass codeProtocol类型字段,您需要稍微修改它。 Change the below and run it and you'll get the information on each USB device by clicking on the item in the tree view (information will appear in the right panel). 更改以下内容并运行它,您将通过单击树视图中的项目获得每个USB设备的信息(信息将显示在右侧面板中)。

Modifications to USB.cs: 对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; }
}

Modifications to fmMain.cs 对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