博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#:基于WMI查询USB设备
阅读量:5963 次
发布时间:2019-06-19

本文共 16009 字,大约阅读时间需要 53 分钟。

来源:http://blog.csdn.net/jhqin/article/details/6734673

/* ----------------------------------------------------------文件名称:WMIUsbQuery.cs作者:秦建辉MSN:splashcn@msn.comQQ: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{    ///     /// 即插即用设备信息结构    ///     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    }        ///     /// 基于WMI获取USB设备信息    ///     public partial class USB    {              #region UsbDevice        ///         /// 获取所有的USB设备实体(过滤没有VID和PID的设备)        ///         public static PnPEntityInfo[] AllUsbDevices        {            get            {                return WhoUsbDevice(UInt16.MinValue, UInt16.MinValue, Guid.Empty);            }        }        ///         /// 查询USB设备实体(设备要求有VID和PID)        ///         /// 供应商标识,MinValue忽视        /// 产品编号,MinValue忽视        /// 设备安装类Guid,Empty忽视        /// 
设备列表
public static PnPEntityInfo[] WhoUsbDevice(UInt16 VendorID, UInt16 ProductID, Guid ClassGuid) { List
UsbDevices = new List
(); // 获取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(); } ///
/// 查询USB设备实体(设备要求有VID和PID) /// ///
供应商标识,MinValue忽视 ///
产品编号,MinValue忽视 ///
设备列表
public static PnPEntityInfo[] WhoUsbDevice(UInt16 VendorID, UInt16 ProductID) { return WhoUsbDevice(VendorID, ProductID, Guid.Empty); } ///
/// 查询USB设备实体(设备要求有VID和PID) /// ///
设备安装类Guid,Empty忽视 ///
设备列表
public static PnPEntityInfo[] WhoUsbDevice(Guid ClassGuid) { return WhoUsbDevice(UInt16.MinValue, UInt16.MinValue, ClassGuid); } ///
/// 查询USB设备实体(设备要求有VID和PID) /// ///
设备ID,可以是不完整信息 ///
设备列表
public static PnPEntityInfo[] WhoUsbDevice(String PNPDeviceID) { List
UsbDevices = new List
(); // 获取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(); } ///
/// 根据服务定位USB设备 /// ///
要查询的服务集合 ///
设备列表
public static PnPEntityInfo[] WhoUsbDevice(String[] ServiceCollection) { if (ServiceCollection == null || ServiceCollection.Length == 0) return WhoUsbDevice(UInt16.MinValue, UInt16.MinValue, Guid.Empty); List
UsbDevices = new List
(); // 获取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 ///
/// 所有即插即用设备实体(过滤没有VID和PID的设备) /// public static PnPEntityInfo[] AllPnPEntities { get { return WhoPnPEntity(UInt16.MinValue, UInt16.MinValue, Guid.Empty); } } ///
/// 根据VID和PID及设备安装类GUID定位即插即用设备实体 /// ///
供应商标识,MinValue忽视 ///
产品编号,MinValue忽视 ///
设备安装类Guid,Empty忽视 ///
设备列表
///
/// 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} ///
public static PnPEntityInfo[] WhoPnPEntity(UInt16 VendorID, UInt16 ProductID, Guid ClassGuid) { List
PnPEntities = new List
(); // 枚举即插即用设备实体 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(); } ///
/// 根据VID和PID定位即插即用设备实体 /// ///
供应商标识,MinValue忽视 ///
产品编号,MinValue忽视 ///
设备列表
public static PnPEntityInfo[] WhoPnPEntity(UInt16 VendorID, UInt16 ProductID) { return WhoPnPEntity(VendorID, ProductID, Guid.Empty); } ///
/// 根据设备安装类GUID定位即插即用设备实体 /// ///
设备安装类Guid,Empty忽视 ///
设备列表
public static PnPEntityInfo[] WhoPnPEntity(Guid ClassGuid) { return WhoPnPEntity(UInt16.MinValue, UInt16.MinValue, ClassGuid); } ///
/// 根据设备ID定位设备 /// ///
设备ID,可以是不完整信息 ///
设备列表
///
/// 注意:对于下划线,需要写成“[_]”,否则视为任意字符 ///
public static PnPEntityInfo[] WhoPnPEntity(String PNPDeviceID) { List
PnPEntities = new List
(); // 枚举即插即用设备实体 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(); } ///
/// 根据服务定位设备 /// ///
要查询的服务集合,null忽视 ///
设备列表
///
/// 跟服务相关的类: /// Win32_SystemDriverPNPEntity /// Win32_SystemDriver ///
public static PnPEntityInfo[] WhoPnPEntity(String[] ServiceCollection) { if (ServiceCollection == null || ServiceCollection.Length == 0) return WhoPnPEntity(UInt16.MinValue, UInt16.MinValue, Guid.Empty); List
PnPEntities = new List
(); // 枚举即插即用设备实体 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 }}

 

你可能感兴趣的文章
C# 自定义控件入门
查看>>
git改密码出现授权问题
查看>>
Hadoop IO 特性详解(2)
查看>>
ORA-02266: 表中的唯一/主键被启用的外键引用
查看>>
MySQL类型转换 使用CAST将varchar转换成int类型排序
查看>>
Django的POST请求时因为开启防止csrf,报403错误,及四种解决方法
查看>>
Apache common-fileupload用户指南
查看>>
day-6 and day-7:面向对象
查看>>
IE维护(IEM)策略不再适用于IE10及后续IE版本
查看>>
Java7中的ForkJoin并发框架初探(下)—— ForkJoin的应用
查看>>
java中的重量级与轻量级概念
查看>>
Linux设备驱动工程师之路——硬件访问及混杂设备LED驱动
查看>>
进程和线程<一>
查看>>
远程算数程序——版本v1.0
查看>>
Mysql常见四种索引的使用
查看>>
说说Android桌面(Launcher应用)背后的故事(一)——揭开她神秘的面纱
查看>>
第一篇:zc706 开箱及开发环境搭建
查看>>
python-冒泡排序
查看>>
Mac下修改Hosts文件工具——Gas Mask
查看>>
协程函数应用
查看>>