如何使用WMI从USB驱动器获取数字vid / pid和驱动器号

时间:2019-07-05 15:29:54

标签: c# usb wmi drive

当我将其连接到计算机时,我试图显示vid / pid和USB驱动器的驱动器号。实际上,我既可以做到,又可以同时做到。

1:我可以使用Win32_DiskDrive转到Win32_LogicalDisk并提取驱动器号。但是deviceId具有我不能使用的非数字pid / vid。

2:我可以使用Win32_USBHubWin32_PnPEntity提取vid / pid,但找不到它们与驱动器号之间的链接。

public void CheckForUsbDevice()
        {
            string text =
                "SELECT * FROM __InstanceCreationEvent " +
                "WITHIN 2 "
              + "WHERE TargetInstance ISA 'Win32_PnPEntity'";


            ManagementEventWatcher watcher = new ManagementEventWatcher();
            WqlEventQuery query = new WqlEventQuery(text);

            watcher.EventArrived += new EventArrivedEventHandler(watcher_EventArrived);

            watcher.Query = query;
            watcher.Start();

            Console.ReadKey();
        }

        static readonly Guid GUID_DEVCLASS_USB = new Guid("{36fc9e60-c465-11cf-8056-444553540000}");

        static void watcher_EventArrived(object sender, EventArrivedEventArgs e)
        {
            ManagementBaseObject instance = (ManagementBaseObject)e.NewEvent["TargetInstance"];
            if ((new Guid((string)instance["ClassGuid"]) == GUID_DEVCLASS_USB) && ((string)instance.Properties["Name"].Value == "USB Mass Storage Device"))
            {
                // we're only interested by USB devices, dump all props
                foreach (var property in instance.Properties)
                {
                    Console.WriteLine(property.Name + " = " + property.Value);
                }
            }
        }

其输出为:

Availability =
Caption = USB Mass Storage Device
ClassGuid = {36fc9e60-c465-11cf-8056-444553540000}
CompatibleID = System.String[]
ConfigManagerErrorCode = 0
ConfigManagerUserConfig = False
CreationClassName = Win32_PnPEntity
Description = USB Mass Storage Device
DeviceID = USB\VID_0911&PID_1F40&MI_00\6&27CAD51B&0&0000
ErrorCleared =
ErrorDescription =
HardwareID = System.String[]
InstallDate =
LastErrorCode =
Manufacturer = Compatible USB storage device
Name = USB Mass Storage Device
PNPClass = USB
PNPDeviceID = USB\VID_0911&PID_1F40&MI_00\6&27CAD51B&0&0000
PowerManagementCapabilities =
PowerManagementSupported =
Present = True
Service = USBSTOR
Status = OK
StatusInfo =
SystemCreationClassName = Win32_ComputerSystem
SystemName = WINDEV1905EVAL

我需要在包含数字pid / vid的WMI分类与驱动器号之间建立链接,或者以其他方式实现该链接。

我认为可以使用DeviceIoControl,但我不知道从哪里开始。

我们非常感谢您的帮助。

0 个答案:

没有答案