c#使用wmi查询usb设备信息示例

 更新时间:2020年6月25日 11:37  点击:2004

开发环境:Visual Studio V2010 .NET Framework 4 Client Profile

复制代码 代码如下:

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
}
}

[!--infotagslink--]

相关文章

  • c#检测usb设备拨插类库USBClassLibrary分享

    这篇文章主要介绍了c#检测usb设备拨插类库USBClassLibrary的简单示例,需要的朋友可以参考下...2020-06-25
  • Android Studio真机无线连接USB设备调试运行详解流程

    你在Android Studio写app时是否也有想过如果可以不用数据线连接手机调试运行就好了?如果需要取出数据线插接的话我肯定是嫌麻烦的,但是模拟器有时候需要测试一些需要硬件支持的功能时又不管用,所以最好的测试还是在真机上,本篇教你扔掉数据线来无线调试...2021-11-04
  • WMI获取硬件信息封装函数方法(联想台式机出厂编号 CPUID BIOS序列号 硬盘信息 显卡信息 MAC地址)

    这篇文章主要介绍了WMI获取硬件信息的方法,硬件信息有:联想台式机出厂编号 CPUID BIOS序列号 硬盘信息 显卡信息 MAC地址...2020-06-25
  • C#获取USB事件API实例分析

    这篇文章主要介绍了C#获取USB事件API,实例分析了C#操作USB事件的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • 电脑USB给智能手机充电是否安全?是否耐用?

    这篇文章主要针对电脑USB给智能手机充电是否安全?是否耐用?两大问题进行解答,对大家长时间以来的疑惑进行分析,答案可能会出乎意料,感兴趣的小伙伴们可以参考一下...2016-07-04
  • js获取USB扫码枪数据的方法

    这篇文章主要为大家详细介绍了js获取USB扫码枪数据的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-29
  • usb口充电插排插座给手机充电会伤到手机吗?

    usb充电插排插座给手机充电会伤到手机吗?现在很多朋友买的插座都带usb接口,给手机充电的时候就直接用插座上的usb接口充电了,长时间使用会给手机带来伤害吗?下面我们来看看详细的内容...2016-07-04
  • 手机数据线Mini USB与micro USB有什么区别?

    手机数据线Mini USB与micro USB有什么区别?手机数据线有Mini USB与micro USB两种,但是这两种有什么相同不区别呢?下面我们来看看怎么选择手机数据线,需要的朋友可以参考下...2016-07-04
  • c#使用wmi查询usb设备信息示例

    这篇文章主要介绍了c#使用wmi查询usb设备信息示例,大家参考使用吧...2020-06-25
  • USB手机数据线实用省钱保护方法

    iPhone数据线接口处开裂一直都是果粉们的心病。一根正常使用的数据线,大约一年左右就不行了。轻微开裂的数据线影响美观性,严重开裂的数据线就一定要换一根新的了,那在日常中我们该如何保养呢...2016-11-01
  • 红米3S怎么打开usb调试 红米3S打开usb调试方法图文教程

    由于红米3S内置的MIUI 7系统默认情况下是找不到USB调试开关的,只有开启开发者选项才能找到入口。很多朋友都不清楚红米3S怎么打开usb调试 ,下面小编给大家带来了红米3S打开usb调试方法,非常不错,感兴趣的朋友可以参考下...2016-07-04
  • 详解WMI RPC 服务器不可用的解决方案

    这篇文章主要介绍了详解WMI RPC 服务器不可用的解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-25
  • USB或将被取代:英特尔将Thunderbolt接口集成进CPU

    英特尔就在推广高速接口Thunderbolt。但除非你购买Mac或者其它高端PC,否则就不要多想了...2017-07-06
  • C语言实现模拟USB对8bit数据的NRZI编码输出

    今天小编就为大家分享一篇关于C语言实现模拟USB对8bit数据的NRZI编码输出,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧...2020-04-25
  • 利用C#操作WMI指南

    WMI提供了一套内置在Microsoft Windows操作系统中的丰富的系统管理服务,可以在有大量的应用程序、服务和设备的系统中提供全方位的管理功能。它允许应用程序的开发者,使用简单的、一致的机制,去查询企业中的任一台计算机上的信息,或是进行系统配置...2020-06-25
  • OpenCV外接USB摄像头的方法

    这篇文章主要为大家详细介绍了OpenCV外接USB摄像头的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-04-25
  • C#调用USB摄像头的方法

    这篇文章主要为大家详细介绍了C#调用USB摄像头的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2022-03-27
  • java实现模拟USB接口的功能

    本文主要介绍了java实现模拟USB接口的功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2022-07-21