C#检测USB设备ClassCode(USB设备类型) [英] C# detect usb device ClassCode (usb device type)

查看:1597
本文介绍了C#检测USB设备ClassCode(USB设备类型)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道当前系统中使用的一种USB设备。有了解 USB设备类代码的 USB规范。但我不能让设备类型,WMI请求 WQL:SELECT * FROM Win32_UsbHub 的类代码,子类代码,协议类型字段给空值。 ?任何想法如何检测目前使用的USB设备类型



我当前的代码:

  ManagementObjectCollection收集;使用
(VAR搜索=新ManagementObjectSearcher(@SELECT * FROM Win32_US​​BHub))
{
集合= searcher.Get();
的foreach(在收集VAR设备)
{
VAR的DeviceID =(字符串)GetPropertyValue(的DeviceID);
VAR pnpDeviceId =(字符串)GetPropertyValue(PNPDeviceID);
VAR DESCR =(字符串)device.GetPropertyValue(说明);
变种classCode = device.GetPropertyValue(ClassCode); //空在这里
}
}


解决方案

您可以下载 USB查看源文件为出发点。这遍历在PC(C#)所有USB设备,并拉起每个信息。要获得类代码子类代码协议类型字段,你需要稍微修改它。更改下方并运行它,你会点击该项目在树视图(信息将显示在右侧面板中)获得每个USB设备上的信息。



的修改USB.cs:

  //将以下属性添加到USBDevice类
//保留一切就像
公共字节DEVICECLASS
{
{返回DeviceDescriptor.bDeviceClass; }
}

公共字节DeviceSubClass
{
{返回DeviceDescriptor.bDeviceSubClass; }
}

公共字节DeviceProtocol
{
{返回DeviceDescriptor.bDeviceProtocol; }
}



的修改fmMain.cs

  //添加ProcessHub功能
内以下行//了如果(port.IsDeviceConnected)语句$ b内$ b //保留一切作为为
如果(port.IsDeviceConnected)
{
// ...
sb​​.AppendLine(的SerialNumber =+ device.SerialNumber) ;
//添加这三行
sb.AppendLine(DEVICECLASS = 0X+ device.DeviceClass.ToString(X));
sb.AppendLine(DeviceSubClass = 0X+ device.DeviceSubClass.ToString(X));
sb.AppendLine(DeviceProtocol = 0X+ device.DeviceProtocol.ToString(X));
// ...
}


I need to know what kind of USB devices currently used in system. There is a USB specification about class codes of USB devices. But I cant get device type, WMI request WQL: select * from Win32_UsbHub give null values on Class code, Subclass code, Protocol type fields. Any ideas how to detect USB device type currently in use?

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. This loops through all USB devices on a PC (C#) and pulls information about each. To get the Class code, Subclass code, and Protocol type fields, you'll need to modify it slightly. 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).

Modifications to 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

// 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"));
   // ...
}

这篇关于C#检测USB设备ClassCode(USB设备类型)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆