c#應用wmi查詢usb裝備信息示例。本站提示廣大學習愛好者:(c#應用wmi查詢usb裝備信息示例)文章只能為提供參考,不一定能成為您想要的結果。以下是c#應用wmi查詢usb裝備信息示例正文
開辟情況: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.WordStr('\\', '_') + "%'";
}
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
}
}