WMI方式

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;      // 设备IDpublic 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){   // 获取设备实体的DeviceIDString 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);    // 设备安装类GUIDif (ClassGuid != Guid.Empty && ClassGuid != theClassGuid) continue;PnPEntityInfo Element;Element.PNPDeviceID = Entity["PNPDeviceID"] as String;  // 设备IDElement.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;   // 设备安装类GUIDUsbDevices.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){   // 获取设备实体的DeviceIDString 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;  // 设备IDElement.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);            // 设备安装类GUIDUsbDevices.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){   // 获取设备实体的DeviceIDString 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;  // 设备IDElement.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);            // 设备安装类GUIDUsbDevices.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[_]____%'";elseVIDPID = "'%VID[_]____&PID[_]" + ProductID.ToString("X4") + "%'";       }else{if (ProductID == UInt16.MinValue)VIDPID = "'%VID[_]" + VendorID.ToString("X4") + "&PID[_]____%'";elseVIDPID = "'%VID[_]" + VendorID.ToString("X4") + "&PID[_]" + ProductID.ToString("X4") + "%'";}String QueryString;if (ClassGuid == Guid.Empty)QueryString = "SELECT * FROM Win32_PnPEntity WHERE PNPDeviceID LIKE" + VIDPID;elseQueryString = "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;                      // 设备IDElement.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);            // 设备安装类GUIDPnPEntities.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;                   // 设备IDElement.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);            // 设备安装类GUIDPnPEntities.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;                      // 设备IDElement.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);            // 设备安装类GUIDPnPEntities.Add(Element);break;}}}}if (PnPEntities.Count == 0) return null; else return PnPEntities.ToArray();}#endregion        }
}

通过API的方式详见

http://www.cnblogs.com/xidongs/archive/2011/11/28/2266100.html

转载于:https://www.cnblogs.com/Kconnie/p/4675156.html

C# 获取USB设备信息相关推荐

  1. 基于WMI获取USB设备信息(即获取插即用设备信息)System.Management.ManagementObjectSearcher--ManagementObjectCollection

    基于WMI获取USB设备信息(即获取插即用设备信息)System.Management.ManagementObjectSearcher--ManagementObjectCollection 获取P ...

  2. Android获取USB设备信息

    一.通过路径查询 cat /proc/bus/input/devices 二.使用UsbManager获取插入手机的USB设备名字 private void getDevice() {UsbManag ...

  3. 用python获取usb设备端口号,用Python查询连接的USB设备信息的简单方法?

    如何在Python中查询连接的USB设备信息? 我想得到UID设备名(例如:SonyEricsson W660),设备路径(例如:dev/ttyACM0) 此外,上述信息中的最佳参数是什么,以便在设备 ...

  4. libusb获取usb设备的idVendor(vid),idProduct(pid),以及Serial Number

    发表于2015/6/23 21:55:11  4594人阅读 最近在做关于usb设备的项目,用到了libusb,发现关于这个的函数库的介绍,讲解很少,下面仅仅是简单展示一些基本的使用方法,以备后用. ...

  5. android 安卓APP获取手机设备信息和手机号码的代码示例

    下面我从安卓开发的角度,简单写一下如何获取手机设备信息和手机号码 准备条件:一部安卓手机.手机SIM卡确保插入手机里.eclipse ADT和android-sdk开发环境 第一步:新建一个andro ...

  6. android app 手机号码,android 安卓APP获取手机设备信息和手机号码的代码示例 .

    下面我从安卓开发的角度,简单写一下如何获取手机设备信息和手机号码 准备条件:一部安卓手机.手机SIM卡确保插入手机里.eclipse ADT和android-sdk开发环境 第一步:新建一个andro ...

  7. android app 手机号码,android 安卓APP获取手机设备信息和手机号码的代码示例

    下面我从安卓开发的角度,简单写一下如何获取手机设备信息和手机号码 准备条件:一部安卓手机.手机SIM卡确保插入手机里.eclipse ADT和android-sdk开发环境 第一步:新建一个andro ...

  8. 如何获取手机设备信息和手机号码

    下面我从安卓开发的角度,简单写一下如何获取手机设备信息和手机号码 准备条件:一部安卓手机.手机SIM卡确保插入手机里.eclipse ADT和Android-sdk开发环境 第一步:新建一个andro ...

  9. python 客户端 如何获取手机_Python学习---Django的request扩展[获取用户设备信息]

    关于Django的request扩展[获取用户设备信息] settings.py INSTALLED_APPS = [ ... 'app01', # 注册app ] STATICFILES_DIRS ...

最新文章

  1. python 网络爬虫学习笔记(一)
  2. 添加当前文件夹及其子文件夹到以及别而的文件夹到当前路径
  3. 【分类算法】Logistic算法原理、标准评分卡开发流程、python代码案例
  4. UML大战需求分析--阅读笔记02
  5. php 查询数据库返回json数据
  6. html语言简单,简单的html语言计算器
  7. 一个会定时完成的任务(二)
  8. python——作用域 == is
  9. python s d是什意思_python里d是什么意思
  10. 中学生计算机编程教学视频6,计算机科学速成课6:寄存器和内存【视频】
  11. 浏览器滚动条默认样式改变
  12. ResNet网络结构搭建
  13. Ubuntu硬盘的分区、格式化、挂载
  14. 华为p20位置服务器在哪打开,华为p20在哪里打开北斗定位 | 手游网游页游攻略大全...
  15. 【OpenCV】图像多通道混合、缩放
  16. 触摸开关模块,TTP223
  17. Dataset:White Wine Quality白葡萄酒品质数据集的简介、下载、使用方法之详细攻略
  18. 一、判别一个分解的无损连接性
  19. 方特主题公园体验科幻神奇
  20. C语言小白初试 7-1 求一元二次方程的根 (20 分)

热门文章

  1. YII 测试环境搭建
  2. setjmp()、longjmp() Linux Exception Handling/Error Handling、no-local goto
  3. [开心]很搞笑的贴图,必看(收藏)
  4. dos命令关闭所有dos窗口
  5. Kotlin——初级篇(六):空类型、空安全、非空断言、类型转换等特性总结
  6. mongoose多条件模糊查询实例
  7. sqlserver 循环赋值变量
  8. 如何解决机器学习中数据不平衡问题
  9. AS2的MD5/SHA1/TEA等加密算法类
  10. docker 虚拟机搭建mongodb一主一从一复制_Docker最全教程——MongoDB容器化(十三)...