using System;
using System.Management;
using System.Text.RegularExpressions;
using System.Collections.Generic;
namespace FstScandTest
{
public struct PnPEntityInfo
{
public String PNPDeviceID;
public String Name;
public String Description;
public String Service;
public String Status;
public UInt16 VendorID;
public UInt16 ProductID;
public Guid ClassGuid;
}
public partial class USB
{
#region UsbDevice
public static PnPEntityInfo[] AllUsbDevices
{
get
{
return WhoUsbDevice(UInt16.MinValue, UInt16.MinValue, Guid.Empty);
}
}
public static string GetClassGuid(string USBName)
{
string classGuid ="";
PnPEntityInfo[] p = WhoUsbDevice(UInt16.MinValue, UInt16.MinValue, Guid.Empty);
foreach (PnPEntityInfo e in p)
{
if (e.Name == USBName)
{
classGuid = e.ClassGuid.ToString();
}
}
return classGuid;
}
public static PnPEntityInfo[] WhoUsbDevice(UInt16 VendorID, UInt16 ProductID, Guid ClassGuid)
{
List<PnPEntityInfo> UsbDevices = new List<PnPEntityInfo>();
ManagementObjectCollection USBControllerDeviceCollection = new ManagementObjectSearcher("SELECT * FROM Win32_USBControllerDevice").Get();
if (USBControllerDeviceCollection != null)
{
foreach (ManagementObject USBControllerDevice in USBControllerDeviceCollection)
{
String Dependent = (USBControllerDevice["Dependent"] as String).Split(new Char[] { '=' })[1];
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);
if (ClassGuid != Guid.Empty && ClassGuid != theClassGuid) continue;
PnPEntityInfo Element;
Element.PNPDeviceID = Entity["PNPDeviceID"] as String;
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;
UsbDevices.Add(Element);
}
}
}
}
}
if (UsbDevices.Count == 0) return null; else return UsbDevices.ToArray();
}
public static PnPEntityInfo[] WhoUsbDevice(UInt16 VendorID, UInt16 ProductID)
{
return WhoUsbDevice(VendorID, ProductID, Guid.Empty);
}
public static PnPEntityInfo[] WhoUsbDevice(Guid ClassGuid)
{
return WhoUsbDevice(UInt16.MinValue, UInt16.MinValue, ClassGuid);
}
public static PnPEntityInfo[] WhoUsbDevice(String PNPDeviceID)
{
List<PnPEntityInfo> UsbDevices = new List<PnPEntityInfo>();
ManagementObjectCollection USBControllerDeviceCollection = new ManagementObjectSearcher("SELECT * FROM Win32_USBControllerDevice").Get();
if (USBControllerDeviceCollection != null)
{
foreach (ManagementObject USBControllerDevice in USBControllerDeviceCollection)
{
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;
}
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;
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);
UsbDevices.Add(Element);
}
}
}
}
}
if (UsbDevices.Count == 0) return null; else return UsbDevices.ToArray();
}
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>();
ManagementObjectCollection USBControllerDeviceCollection = new ManagementObjectSearcher("SELECT * FROM Win32_USBControllerDevice").Get();
if (USBControllerDeviceCollection != null)
{
foreach (ManagementObject USBControllerDevice in USBControllerDeviceCollection)
{
String Dependent = (USBControllerDevice["Dependent"] as String).Split(new Char[] { '=' })[1];
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;
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);
UsbDevices.Add(Element);
break;
}
}
}
}
}
}
if (UsbDevices.Count == 0) return null; else return UsbDevices.ToArray();
}
#endregion
#region PnPEntity
public static PnPEntityInfo[] AllPnPEntities
{
get
{
return WhoPnPEntity(UInt16.MinValue, UInt16.MinValue, Guid.Empty);
}
}
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;
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);
PnPEntities.Add(Element);
}
}
}
if (PnPEntities.Count == 0) return null; else return PnPEntities.ToArray();
}
public static PnPEntityInfo[] WhoPnPEntity(UInt16 VendorID, UInt16 ProductID)
{
return WhoPnPEntity(VendorID, ProductID, Guid.Empty);
}
public static PnPEntityInfo[] WhoPnPEntity(Guid ClassGuid)
{
return WhoPnPEntity(UInt16.MinValue, UInt16.MinValue, ClassGuid);
}
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
{
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;
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);
PnPEntities.Add(Element);
}
}
}
if (PnPEntities.Count == 0) return null; else return PnPEntities.ToArray();
}
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;
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);
PnPEntities.Add(Element);
break;
}
}
}
}
if (PnPEntities.Count == 0) return null; else return PnPEntities.ToArray();
}
#endregion
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)