C#:基于WMI查询USB设备信息 及 Android设备厂商VID列表
2023-09-14 08:56:59 时间
/* ---------------------------------------------------------- 文件名称:WMIUsbQuery.cs 作者:秦建辉 MSN:splashcn@msn.com QQ:36748897 博客:http://blog.csdn.net/jhqin 开发环境: Visual Studio V2010 .NET Framework 4 Client Profile 版本历史: V1.3 2011年09月08日 代码优化 V1.2 2011年09月02日 增加基于服务的查询 V1.1 2011年09月01日 增加基于设备ID的查询,解决LIKE子句中反斜杠字符引发的WQL查询异常 V1.0 2011年08月30日 基于WMI实现对USB设备的查询 ------------------------------------------------------------ */ using System; using System.Management; using System.Text.RegularExpressions; using System.Collections.Generic; namespace Splash.IO.PORTS { /// <summary> /// 即插即用设备信息结构 /// </summary> public struct PnPEntityInfo { public String PNPDeviceID; // 设备ID public String Name; // 设备名称 public String Description; // 设备描述 public String Service; // 服务 public String Status; // 设备状态 public UInt16 VendorID; // 供应商标识 public UInt16 ProductID; // 产品编号 public Guid ClassGuid; // 设备安装类GUID } /// <summary> /// 基于WMI获取USB设备信息 /// </summary> public partial class USB { #region UsbDevice /// <summary> /// 获取所有的USB设备实体(过滤没有VID和PID的设备) /// </summary> public static PnPEntityInfo[] AllUsbDevices { get { return WhoUsbDevice(UInt16.MinValue, UInt16.MinValue, Guid.Empty); } } /// <summary> /// 查询USB设备实体(设备要求有VID和PID) /// </summary> /// <param name="VendorID">供应商标识,MinValue忽视</param> /// <param name="ProductID">产品编号,MinValue忽视</param> /// <param name="ClassGuid">设备安装类Guid,Empty忽视</param> /// <returns>设备列表</returns> public static PnPEntityInfo[] WhoUsbDevice(UInt16 VendorID, UInt16 ProductID, Guid ClassGuid) { List<PnPEntityInfo> UsbDevices = new List<PnPEntityInfo>(); // 获取USB控制器及其相关联的设备实体 ManagementObjectCollection USBControllerDeviceCollection = new ManagementObjectSearcher("SELECT * FROM Win32_USBControllerDevice").Get(); if (USBControllerDeviceCollection != null) { foreach (ManagementObject USBControllerDevice in USBControllerDeviceCollection) { // 获取设备实体的DeviceID String Dependent = (USBControllerDevice["Dependent"] as String).Split(new Char[] { '=' })[1]; // 过滤掉没有VID和PID的USB设备 Match match = Regex.Match(Dependent, "VID_[0-9|A-F]{4}&PID_[0-9|A-F]{4}"); if (match.Success) { UInt16 theVendorID = Convert.ToUInt16(match.Value.Substring(4, 4), 16); // 供应商标识 if (VendorID != UInt16.MinValue && VendorID != theVendorID) continue; UInt16 theProductID = Convert.ToUInt16(match.Value.Substring(13, 4), 16); // 产品编号 if (ProductID != UInt16.MinValue && ProductID != theProductID) continue; ManagementObjectCollection PnPEntityCollection = new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity WHERE DeviceID=" + Dependent).Get(); if (PnPEntityCollection != null) { foreach (ManagementObject Entity in PnPEntityCollection) { Guid theClassGuid = new Guid(Entity["ClassGuid"] as String); // 设备安装类GUID if (ClassGuid != Guid.Empty && ClassGuid != theClassGuid) continue; PnPEntityInfo Element; Element.PNPDeviceID = Entity["PNPDeviceID"] as String; // 设备ID Element.Name = Entity["Name"] as String; // 设备名称 Element.Description = Entity["Description"] as String; // 设备描述 Element.Service = Entity["Service"] as String; // 服务 Element.Status = Entity["Status"] as String; // 设备状态 Element.VendorID = theVendorID; // 供应商标识 Element.ProductID = theProductID; // 产品编号 Element.ClassGuid = theClassGuid; // 设备安装类GUID UsbDevices.Add(Element); } } } } } if (UsbDevices.Count == 0) return null; else return UsbDevices.ToArray(); } /// <summary> /// 查询USB设备实体(设备要求有VID和PID) /// </summary> /// <param name="VendorID">供应商标识,MinValue忽视</param> /// <param name="ProductID">产品编号,MinValue忽视</param> /// <returns>设备列表</returns> public static PnPEntityInfo[] WhoUsbDevice(UInt16 VendorID, UInt16 ProductID) { return WhoUsbDevice(VendorID, ProductID, Guid.Empty); } /// <summary> /// 查询USB设备实体(设备要求有VID和PID) /// </summary> /// <param name="ClassGuid">设备安装类Guid,Empty忽视</param> /// <returns>设备列表</returns> public static PnPEntityInfo[] WhoUsbDevice(Guid ClassGuid) { return WhoUsbDevice(UInt16.MinValue, UInt16.MinValue, ClassGuid); } /// <summary> /// 查询USB设备实体(设备要求有VID和PID) /// </summary> /// <param name="PNPDeviceID">设备ID,可以是不完整信息</param> /// <returns>设备列表</returns> public static PnPEntityInfo[] WhoUsbDevice(String PNPDeviceID) { List<PnPEntityInfo> UsbDevices = new List<PnPEntityInfo>(); // 获取USB控制器及其相关联的设备实体 ManagementObjectCollection USBControllerDeviceCollection = new ManagementObjectSearcher("SELECT * FROM Win32_USBControllerDevice").Get(); if (USBControllerDeviceCollection != null) { foreach (ManagementObject USBControllerDevice in USBControllerDeviceCollection) { // 获取设备实体的DeviceID String Dependent = (USBControllerDevice["Dependent"] as String).Split(new Char[] { '=' })[1]; if (!String.IsNullOrEmpty(PNPDeviceID)) { // 注意:忽视大小写 if (Dependent.IndexOf(PNPDeviceID, 1, PNPDeviceID.Length - 2, StringComparison.OrdinalIgnoreCase) == -1) continue; } // 过滤掉没有VID和PID的USB设备 Match match = Regex.Match(Dependent, "VID_[0-9|A-F]{4}&PID_[0-9|A-F]{4}"); if (match.Success) { ManagementObjectCollection PnPEntityCollection = new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity WHERE DeviceID=" + Dependent).Get(); if (PnPEntityCollection != null) { foreach (ManagementObject Entity in PnPEntityCollection) { PnPEntityInfo Element; Element.PNPDeviceID = Entity["PNPDeviceID"] as String; // 设备ID Element.Name = Entity["Name"] as String; // 设备名称 Element.Description = Entity["Description"] as String; // 设备描述 Element.Service = Entity["Service"] as String; // 服务 Element.Status = Entity["Status"] as String; // 设备状态 Element.VendorID = Convert.ToUInt16(match.Value.Substring(4, 4), 16); // 供应商标识 Element.ProductID = Convert.ToUInt16(match.Value.Substring(13, 4), 16); // 产品编号 // 产品编号 Element.ClassGuid = new Guid(Entity["ClassGuid"] as String); // 设备安装类GUID UsbDevices.Add(Element); } } } } } if (UsbDevices.Count == 0) return null; else return UsbDevices.ToArray(); } /// <summary> /// 根据服务定位USB设备 /// </summary> /// <param name="ServiceCollection">要查询的服务集合</param> /// <returns>设备列表</returns> public static PnPEntityInfo[] WhoUsbDevice(String[] ServiceCollection) { if (ServiceCollection == null || ServiceCollection.Length == 0) return WhoUsbDevice(UInt16.MinValue, UInt16.MinValue, Guid.Empty); List<PnPEntityInfo> UsbDevices = new List<PnPEntityInfo>(); // 获取USB控制器及其相关联的设备实体 ManagementObjectCollection USBControllerDeviceCollection = new ManagementObjectSearcher("SELECT * FROM Win32_USBControllerDevice").Get(); if (USBControllerDeviceCollection != null) { foreach (ManagementObject USBControllerDevice in USBControllerDeviceCollection) { // 获取设备实体的DeviceID String Dependent = (USBControllerDevice["Dependent"] as String).Split(new Char[] { '=' })[1]; // 过滤掉没有VID和PID的USB设备 Match match = Regex.Match(Dependent, "VID_[0-9|A-F]{4}&PID_[0-9|A-F]{4}"); if (match.Success) { ManagementObjectCollection PnPEntityCollection = new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity WHERE DeviceID=" + Dependent).Get(); if (PnPEntityCollection != null) { foreach (ManagementObject Entity in PnPEntityCollection) { String theService = Entity["Service"] as String; // 服务 if (String.IsNullOrEmpty(theService)) continue; foreach (String Service in ServiceCollection) { // 注意:忽视大小写 if (String.Compare(theService, Service, true) != 0) continue; PnPEntityInfo Element; Element.PNPDeviceID = Entity["PNPDeviceID"] as String; // 设备ID Element.Name = Entity["Name"] as String; // 设备名称 Element.Description = Entity["Description"] as String; // 设备描述 Element.Service = theService; // 服务 Element.Status = Entity["Status"] as String; // 设备状态 Element.VendorID = Convert.ToUInt16(match.Value.Substring(4, 4), 16); // 供应商标识 Element.ProductID = Convert.ToUInt16(match.Value.Substring(13, 4), 16); // 产品编号 Element.ClassGuid = new Guid(Entity["ClassGuid"] as String); // 设备安装类GUID UsbDevices.Add(Element); break; } } } } } } if (UsbDevices.Count == 0) return null; else return UsbDevices.ToArray(); } #endregion #region PnPEntity /// <summary> /// 所有即插即用设备实体(过滤没有VID和PID的设备) /// </summary> public static PnPEntityInfo[] AllPnPEntities { get { return WhoPnPEntity(UInt16.MinValue, UInt16.MinValue, Guid.Empty); } } /// <summary> /// 根据VID和PID及设备安装类GUID定位即插即用设备实体 /// </summary> /// <param name="VendorID">供应商标识,MinValue忽视</param> /// <param name="ProductID">产品编号,MinValue忽视</param> /// <param name="ClassGuid">设备安装类Guid,Empty忽视</param> /// <returns>设备列表</returns> /// <remarks> /// HID:{745a17a0-74d3-11d0-b6fe-00a0c90f57da} /// Imaging Device:{6bdd1fc6-810f-11d0-bec7-08002be2092f} /// Keyboard:{4d36e96b-e325-11ce-bfc1-08002be10318} /// Mouse:{4d36e96f-e325-11ce-bfc1-08002be10318} /// Network Adapter:{4d36e972-e325-11ce-bfc1-08002be10318} /// USB:{36fc9e60-c465-11cf-8056-444553540000} /// </remarks> public static PnPEntityInfo[] WhoPnPEntity(UInt16 VendorID, UInt16 ProductID, Guid ClassGuid) { List<PnPEntityInfo> PnPEntities = new List<PnPEntityInfo>(); // 枚举即插即用设备实体 String VIDPID; if (VendorID == UInt16.MinValue) { if (ProductID == UInt16.MinValue) VIDPID = "'%VID[_]____&PID[_]____%'"; else VIDPID = "'%VID[_]____&PID[_]" + ProductID.ToString("X4") + "%'"; } else { if (ProductID == UInt16.MinValue) VIDPID = "'%VID[_]" + VendorID.ToString("X4") + "&PID[_]____%'"; else VIDPID = "'%VID[_]" + VendorID.ToString("X4") + "&PID[_]" + ProductID.ToString("X4") + "%'"; } String QueryString; if (ClassGuid == Guid.Empty) QueryString = "SELECT * FROM Win32_PnPEntity WHERE PNPDeviceID LIKE" + VIDPID; else QueryString = "SELECT * FROM Win32_PnPEntity WHERE PNPDeviceID LIKE" + VIDPID + " AND ClassGuid='" + ClassGuid.ToString("B") + "'"; ManagementObjectCollection PnPEntityCollection = new ManagementObjectSearcher(QueryString).Get(); if (PnPEntityCollection != null) { foreach (ManagementObject Entity in PnPEntityCollection) { String PNPDeviceID = Entity["PNPDeviceID"] as String; Match match = Regex.Match(PNPDeviceID, "VID_[0-9|A-F]{4}&PID_[0-9|A-F]{4}"); if (match.Success) { PnPEntityInfo Element; Element.PNPDeviceID = PNPDeviceID; // 设备ID Element.Name = Entity["Name"] as String; // 设备名称 Element.Description = Entity["Description"] as String; // 设备描述 Element.Service = Entity["Service"] as String; // 服务 Element.Status = Entity["Status"] as String; // 设备状态 Element.VendorID = Convert.ToUInt16(match.Value.Substring(4, 4), 16); // 供应商标识 Element.ProductID = Convert.ToUInt16(match.Value.Substring(13, 4), 16); // 产品编号 Element.ClassGuid = new Guid(Entity["ClassGuid"] as String); // 设备安装类GUID PnPEntities.Add(Element); } } } if (PnPEntities.Count == 0) return null; else return PnPEntities.ToArray(); } /// <summary> /// 根据VID和PID定位即插即用设备实体 /// </summary> /// <param name="VendorID">供应商标识,MinValue忽视</param> /// <param name="ProductID">产品编号,MinValue忽视</param> /// <returns>设备列表</returns> public static PnPEntityInfo[] WhoPnPEntity(UInt16 VendorID, UInt16 ProductID) { return WhoPnPEntity(VendorID, ProductID, Guid.Empty); } /// <summary> /// 根据设备安装类GUID定位即插即用设备实体 /// </summary> /// <param name="ClassGuid">设备安装类Guid,Empty忽视</param> /// <returns>设备列表</returns> public static PnPEntityInfo[] WhoPnPEntity(Guid ClassGuid) { return WhoPnPEntity(UInt16.MinValue, UInt16.MinValue, ClassGuid); } /// <summary> /// 根据设备ID定位设备 /// </summary> /// <param name="PNPDeviceID">设备ID,可以是不完整信息</param> /// <returns>设备列表</returns> /// <remarks> /// 注意:对于下划线,需要写成“[_]”,否则视为任意字符 /// </remarks> public static PnPEntityInfo[] WhoPnPEntity(String PNPDeviceID) { List<PnPEntityInfo> PnPEntities = new List<PnPEntityInfo>(); // 枚举即插即用设备实体 String QueryString; if (String.IsNullOrEmpty(PNPDeviceID)) { QueryString = "SELECT * FROM Win32_PnPEntity WHERE PNPDeviceID LIKE '%VID[_]____&PID[_]____%'"; } else { // LIKE子句中有反斜杠字符将会引发WQL查询异常 QueryString = "SELECT * FROM Win32_PnPEntity WHERE PNPDeviceID LIKE '%" + PNPDeviceID.Replace('\\', '_') + "%'"; } ManagementObjectCollection PnPEntityCollection = new ManagementObjectSearcher(QueryString).Get(); if (PnPEntityCollection != null) { foreach (ManagementObject Entity in PnPEntityCollection) { String thePNPDeviceID = Entity["PNPDeviceID"] as String; Match match = Regex.Match(thePNPDeviceID, "VID_[0-9|A-F]{4}&PID_[0-9|A-F]{4}"); if (match.Success) { PnPEntityInfo Element; Element.PNPDeviceID = thePNPDeviceID; // 设备ID Element.Name = Entity["Name"] as String; // 设备名称 Element.Description = Entity["Description"] as String; // 设备描述 Element.Service = Entity["Service"] as String; // 服务 Element.Status = Entity["Status"] as String; // 设备状态 Element.VendorID = Convert.ToUInt16(match.Value.Substring(4, 4), 16); // 供应商标识 Element.ProductID = Convert.ToUInt16(match.Value.Substring(13, 4), 16); // 产品编号 Element.ClassGuid = new Guid(Entity["ClassGuid"] as String); // 设备安装类GUID PnPEntities.Add(Element); } } } if (PnPEntities.Count == 0) return null; else return PnPEntities.ToArray(); } /// <summary> /// 根据服务定位设备 /// </summary> /// <param name="ServiceCollection">要查询的服务集合,null忽视</param> /// <returns>设备列表</returns> /// <remarks> /// 跟服务相关的类: /// Win32_SystemDriverPNPEntity /// Win32_SystemDriver /// </remarks> public static PnPEntityInfo[] WhoPnPEntity(String[] ServiceCollection) { if (ServiceCollection == null || ServiceCollection.Length == 0) return WhoPnPEntity(UInt16.MinValue, UInt16.MinValue, Guid.Empty); List<PnPEntityInfo> PnPEntities = new List<PnPEntityInfo>(); // 枚举即插即用设备实体 String QueryString = "SELECT * FROM Win32_PnPEntity WHERE PNPDeviceID LIKE '%VID[_]____&PID[_]____%'"; ManagementObjectCollection PnPEntityCollection = new ManagementObjectSearcher(QueryString).Get(); if (PnPEntityCollection != null) { foreach (ManagementObject Entity in PnPEntityCollection) { String PNPDeviceID = Entity["PNPDeviceID"] as String; Match match = Regex.Match(PNPDeviceID, "VID_[0-9|A-F]{4}&PID_[0-9|A-F]{4}"); if (match.Success) { String theService = Entity["Service"] as String; // 服务 if (String.IsNullOrEmpty(theService)) continue; foreach (String Service in ServiceCollection) { // 注意:忽视大小写 if (String.Compare(theService, Service, true) != 0) continue; PnPEntityInfo Element; Element.PNPDeviceID = PNPDeviceID; // 设备ID Element.Name = Entity["Name"] as String; // 设备名称 Element.Description = Entity["Description"] as String; // 设备描述 Element.Service = theService; // 服务 Element.Status = Entity["Status"] as String; // 设备状态 Element.VendorID = Convert.ToUInt16(match.Value.Substring(4, 4), 16); // 供应商标识 Element.ProductID = Convert.ToUInt16(match.Value.Substring(13, 4), 16); // 产品编号 Element.ClassGuid = new Guid(Entity["ClassGuid"] as String); // 设备安装类GUID PnPEntities.Add(Element); break; } } } } if (PnPEntities.Count == 0) return null; else return PnPEntities.ToArray(); } #endregion } }
Android设备厂商VID列表 :
http://developer.android.com/tools/device.html
Company | USB Vendor ID |
---|---|
Acer | 0502 |
ASUS | 0b05 |
Dell | 413c |
Foxconn | 0489 |
Fujitsu | 04c5 |
Fujitsu Toshiba | 04c5 |
Garmin-Asus | 091e |
18d1 |
|
Haier | 201E |
Hisense | 109b |
HTC | 0bb4 |
Huawei | 12d1 |
K-Touch | 24e3 |
KT Tech | 2116 |
Kyocera | 0482 |
Lenovo | 17ef |
LG | 1004 |
Motorola | 22b8 |
MTK | 0e8d |
NEC | 0409 |
Nook | 2080 |
Nvidia | 0955 |
OTGV | 2257 |
Pantech | 10a9 |
Pegatron | 1d4d |
Philips | 0471 |
PMC-Sierra | 04da |
Qualcomm | 05c6 |
SK Telesys | 1f53 |
Samsung | 04e8 |
Sharp | 04dd |
Sony | 054c |
Sony Ericsson | 0fce |
Teleepoch | 2340 |
Toshiba | 0930 |
ZTE | 19d2 |
相关文章
- android studio不能输入中文_Android模拟器
- android错误_android:layout_margin
- Android telephony_android获取真实时间
- 加密狗android,Android系统加密狗的设计与实现
- android+制作开机动画,Android 开机动画制作详解
- Android HandlerThread 详解
- c#数组赋初值_C#数组初始化
- android app 退出功能,Android 完美退出 App (Exit)
- android 模拟器 haxm,Android模拟器不使用HAXM
- android declare-styleable 和style,Android 关于declare-styleable属性的写法….
- android 短信验证码的实现
- Android preference_android studio preview
- android登录注册跳转的代码_Android开发代码
- Android触摸事件_android设置按钮点击事件
- android activitymanagerservice_安卓开发API
- Android模拟器修改hosts文件详解
- Android Services Library_android freeware
- Android/Linux Root 的那些事儿
- 【Android 安全】DEX 加密 ( Application 替换 | 分析 ContentProvider 组件中调用 getApplication() 获取的 Application 二 )
- 【Android 逆向】Android 权限 ( Android 逆向中使用的 android.permission 权限 | Android 系统中的 Linux 用户权限 )
- 【Android 逆向】ART 脱壳 ( 修改 /art/runtime/dex_file.cc#OpenCommon 系统源码进行脱壳 )
- 【Android Gradle 插件】build.gradle 中的 android 配置 ( 配置项 | compileSdkVersion 配置 | buildToolsVersion 配置 )
- 【ijkplayer】编译 Android 版本的 ijkplayer ② ( 切换到 k0.8.8 分支 | 执行 init-android.sh 脚本进行初始化操作 )
- 【错误记录】Android 应用安全检测漏洞修复 ( StrandHogg 漏洞 | 设置 Activity 组件 android:taskAffinity=““ )
- Android-SQLite数据库操作详解手机开发
- Android开发中遇到的问题(三)——eclipse创建android项目无法正常预览布局文件详解手机开发
- [android] 隐式意图的配置详解手机开发
- Android中android:visibility的3中属性的剖析详解手机开发
- 黑莓Android新机即将登场 Onward Mobility启动“预先承诺”计划
- Android实现PHP连接MySQL进行数据交互(android通过php连接mysql)
- android默认时间格式修改方法
- Android界面刷新的方法分享
- android实现关闭或开启移动网络数据
- Android序列化XML数据
- C#正则表达式分解和转换IP地址实例(C#正则表达式大全c#正则表达式语法)
- android中WebView和javascript实现数据交互实例