C# 获取USB设备信息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; // 设备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[] { '=' })[]; // 过滤掉没有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(, ), ); // 供应商标识
if (VendorID != UInt16.MinValue && VendorID != theVendorID) continue; UInt16 theProductID = Convert.ToUInt16(match.Value.Substring(, ), ); // 产品编号
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 == ) 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[] { '=' })[];
if (!String.IsNullOrEmpty(PNPDeviceID))
{ // 注意:忽视大小写
if (Dependent.IndexOf(PNPDeviceID, , PNPDeviceID.Length - , StringComparison.OrdinalIgnoreCase) == -) 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(, ), ); // 供应商标识
Element.ProductID = Convert.ToUInt16(match.Value.Substring(, ), ); // 产品编号 // 产品编号
Element.ClassGuid = new Guid(Entity["ClassGuid"] as String); // 设备安装类GUID UsbDevices.Add(Element);
}
}
}
}
} if (UsbDevices.Count == ) 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 == )
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[] { '=' })[]; // 过滤掉没有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) != ) 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(, ), ); // 供应商标识
Element.ProductID = Convert.ToUInt16(match.Value.Substring(, ), ); // 产品编号
Element.ClassGuid = new Guid(Entity["ClassGuid"] as String); // 设备安装类GUID UsbDevices.Add(Element);
break;
}
}
}
}
}
} if (UsbDevices.Count == ) 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(, ), ); // 供应商标识
Element.ProductID = Convert.ToUInt16(match.Value.Substring(, ), ); // 产品编号
Element.ClassGuid = new Guid(Entity["ClassGuid"] as String); // 设备安装类GUID PnPEntities.Add(Element);
}
}
} if (PnPEntities.Count == ) 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(, ), ); // 供应商标识
Element.ProductID = Convert.ToUInt16(match.Value.Substring(, ), ); // 产品编号
Element.ClassGuid = new Guid(Entity["ClassGuid"] as String); // 设备安装类GUID PnPEntities.Add(Element);
}
}
} if (PnPEntities.Count == ) 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 == )
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) != ) 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(, ), ); // 供应商标识
Element.ProductID = Convert.ToUInt16(match.Value.Substring(, ), ); // 产品编号
Element.ClassGuid = new Guid(Entity["ClassGuid"] as String); // 设备安装类GUID PnPEntities.Add(Element);
break;
}
}
}
} if (PnPEntities.Count == ) return null; else return PnPEntities.ToArray();
}
#endregion
}
}

调用代码:

 PnPEntityInfo[] deviceList = USB.WhoUsbDevice(DEVICE_VID, DEVICE_PID);//参数:设备的供应商编号,设备产品编号

通过API的方式详见

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

原文链接:https://www.cnblogs.com/Kconnie/p/4675156.html

C# 获取USB设备信息的更多相关文章

  1. Qt 获取usb设备信息 hacking

    /************************************************************************** * Qt 获取usb设备信息 hacking * ...

  2. C#:基于WMI查询USB设备信息 及 Android设备厂商VID列表

    /* ---------------------------------------------------------- 文件名称:WMIUsbQuery.cs 作者:秦建辉 MSN:splashc ...

  3. Windows下USB磁盘开发系列三:枚举系统中U盘、并获取其设备信息

    前面我们介绍了枚举系统中的U盘盘符(见<Windows下USB磁盘开发系列一:枚举系统中U盘的盘符>).以及获取USB设备的信息(见<Windows下USB磁盘开发系列二:枚举系统中 ...

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

    http://blog.csdn.net/changemyself/article/details/7421476 下面我从安卓开发的角度,简单写一下如何获取手机设备信息和手机号码 准备条件:一部安卓 ...

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

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

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

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

  7. ?Object-C获取手机设备信息

    一.获取UiDevice设备信息 // 获取设备名称 NSString *name = [[UIDevice currentDevice] name]; // 获取设备系统名称 NSString *s ...

  8. iOS开发-Object-C获取手机设备信息(UIDevice)

    一.获取UiDevice设备信息 // 获取设备名称 NSString *name = [[UIDevice currentDevice] name]; // 获取设备系统名称 NSString *s ...

  9. Python学习---Django的request扩展[获取用户设备信息]

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

随机推荐

  1. javascript 四舍五入; js 四舍五入

    方法 Math.round round() 方法可把一个数字舍入为最接近的整数. 对于 0.5,该方法将进行上舍入. 例如,3.5 将舍入为 4,而 -3.5 将舍入为 -3. Math.round( ...

  2. Python——迭代器&可迭代对象

    可迭代对象 什么是对象: Python中,一切皆对象.一个变量,一个列表,一个字符串,文件句柄,函数等等都可称为一个对象.一个对象就是一个实例,就是实实在在的东西. 什么是迭代 迭代就是一个重复的过程 ...

  3. log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment)的解决

    报错:log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironme ...

  4. k8s删除pod时,docker服务出现挂载点泄漏问题的解决

    k8s更新版本后,老的POD一直出现Terminating,多久都不能删除. 然后,进入具体的节点机器之后,查看日志输出如下类似: ERROR: driver "overlay" ...

  5. 小程序~获取手机号getPhoneNumber提示该appid没有权限

    处理思路 (1)小程序是不是企业主体 (2)有没有进行认证 (3)如果没有 是不可以获取用户手机号码的 .

  6. laravel 依赖注入

    <?php interface Animal{ public function attack(); public function talk(); } class People implemen ...

  7. 常见的C语言编程规范

    头文件: 1.头文件中适合放置接口的声明,不适合放置实现. 2.头文件应向稳定的方向包含,产品依赖于平台,平台依赖于标准库. 3. .c/.h文件禁止包含用不到的头文件. 4.每一个.c文件应有一个同 ...

  8. wait,waitpid学习测试

    用man wait学习wait waitpid的使用 wait()函数功能:wait()函数使父进程暂停执行,直到它的一个子进程结束为止,该函数的返回值是终止运行的子进程的PID. 参数status所 ...

  9. react 面试指南

    ------------恢复内容开始------------ 什么是声明式编程 声明式编程是一种编程范式,它关注的是你要做什么,而不是如何做.它表达逻辑而不显式地定义步骤.这意味着我们需要根据逻辑的计 ...

  10. hihoCoder #1143 : 骨牌覆盖问题·一(矩阵乘法)

    1143 : 骨牌覆盖问题·一 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 骨牌,一种古老的玩具.今天我们要研究的是骨牌的覆盖问题: 我们有一个2xN的长条形棋盘,然 ...