• 大小: 2.93KB
    文件类型: .7z
    金币: 1
    下载: 0 次
    发布日期: 2024-04-26
  • 语言: C#
  • 标签: pid  usb  sb  UI  

资源简介

USB读取PID和UID

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
using System.Text.Regularexpressions;
using System.Windows.Forms;

namespace test
{
    /// 
    /// 即插即用设备信息结构
    /// 

     
    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
    } 
    /// 
    /// 基于WMI获取USB设备信息
    /// 

    public partial class USB
    {
        #region UsbDevice
        /// 
        /// 获取所有的USB设备实体(过滤没有VID和PID的设备)
        /// 

        public static PnPEntityInfo[] AllUsbDevices
        {
            get
            {
                return WhoUsbDevice(UInt16.MinValue UInt16.MinValue Guid.Empty);
            }
        }

        /// 
        /// 查询USB设备实体(设备要求有VID和PID)
        /// 

        /// 供应商标识,MinValue忽视
        /// 产品编号,MinValue忽视
        /// 设备安装类Guid,Empty忽视
        /// 设备列表

        public static  int numb;//统计usb设备数量          
        public static PnPEntityInfo[] WhoUsbDevice(UInt16 VendorID UInt16 ProductID Guid ClassGuid)
        {
            numb = 0;

            List UsbDevices = new List();

            // 获取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=“ + Depend

评论

共有 条评论