从 USB VID/PID 中查找可移动磁盘的 Windows 驱动器号

Sre*_*dni 2 .net c#

这个问题之前已经问过,并且有一个据说可以在这里工作的答案。但我已经试过了,它对我不起作用。

问题在于 Win32_DiskDrive 上的查询返回的 PNPDeviceID 与“设备”类返回的 PNPDeviceID 不同。例如,在我的情况下,查询返回类似 - PNPDeviceID: USBSTOR\DISK&VEN_ABCD&PROD_1234&REV_0001\8&2C3C9390&0而设备类返回实际的 VID/PID 组合 --> USB\VID_4568&PID_QWER&MI_08\7&100d .

所以 Win32_DiskDrive 上的 SELECT 查询总是失败。

主要代码:

    var usbDevices = GetUSBDevices();

    //Enumerate the USB devices to see if any have specific VID/PID
    foreach (var usbDevice in usbDevices)
    {
        if (usbDevice.DeviceID.Contains("ABCD") && usbDevice.DeviceID.Contains("1234"))
        {
            foreach (string name in usbDevice.GetDiskNames())
            {
                //Open dialog to show file names
                Debug.WriteLine(name);
            }
        }                   
    }
Run Code Online (Sandbox Code Playgroud)

USBDeviceInfo 类

class USBDeviceInfo
{
    public USBDeviceInfo(string deviceID, string pnpDeviceID, string description)
    {
        this.DeviceID = deviceID;
        this.PnpDeviceID = pnpDeviceID;
        this.Description = description;
    }

    public string DeviceID { get; private set; }
    public string PnpDeviceID { get; private set; }
    public string Description { get; private set; }

    public IEnumerable<string> GetDiskNames()
    {
        using (Device device = Device.Get(PnpDeviceID))
        {
            // get children devices
            foreach (string childDeviceId in device.ChildrenPnpDeviceIds)
            {
                // get the drive object that correspond to this id (escape the id)
                Debug.WriteLine(childDeviceId.Replace(@"\", @"\\") );
                foreach (ManagementObject drive in new ManagementObjectSearcher("SELECT DeviceID FROM Win32_DiskDrive WHERE PNPDeviceID='" + childDeviceId.Replace(@"\", @"\\") + "'").Get())
                {

                    foreach (PropertyData usb in drive.Properties){
                        if (usb.Value != null && usb.Value.ToString() != "")
                        {
                            Debug.Write(usb.Name + "=");
                            Debug.Write(usb.Value + "\r\n");
                        }
                    }

                    // associate physical disks with partitions
                    foreach (ManagementObject partition in new ManagementObjectSearcher("ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" + drive["DeviceID"] + "'} WHERE AssocClass=Win32_DiskDriveToDiskPartition").Get())
                    {
                        // associate partitions with logical disks (drive letter volumes)
                        foreach (ManagementObject disk in new ManagementObjectSearcher("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" + partition["DeviceID"] + "'} WHERE AssocClass=Win32_LogicalDiskToPartition").Get())
                        {
                            yield return (string)disk["DeviceID"];
                        }
                    }
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

作为旁注,我能够使用“模型”属性传递获取查询,然后找到驱动器号,如解释here。但我正在寻找一种可以将 VID/PID 与驱动器号联系起来的解决方案。

Mic*_*ela 5

我遇到了完全相同的问题,经过一周的艰苦工作,我终于找到了解决方案。我还获得了一个设备的两个链接(带有 vid 和 pid 的链接以及一个包含“USBSTOR ....”的链接)。我认为这不是解决问题的最佳方法,但(直到现在)它有效。

我使用了两个函数:

第一个是找到具有特定 VID 和 PID 的 USBHub,并找到相关的第二个链接(“USBSTOR....”)。此链接很重要,因为它形成了与驱动器号的连接。名为“USBobjects”的列表包含许多相关链接(USBHub、USBSTOR....),这些链接指向所有连接的设备。我发现,USBSTOR 链接出现在链接之后,其中包含 VID 和 PID。我将“USBStOR...”链接存储为字符串,并将其用于第二个函数以查找 DiskDrive 的相关 PNPEntity。这会导致正确的 DiskPartition 以及 LogicalDisk = 驱动器号。

希望现在还不晚,这两个功能都会对您有所帮助!

第一个功能:

    public void FindPath()
    {
        foreach (ManagementObject entity in new ManagementObjectSearcher("select * from Win32_USBHub Where DeviceID Like '%VID_XXXX&PID_XXXX%'").Get())
        {
            Entity = entity["DeviceID"].ToString();

            foreach (ManagementObject controller in entity.GetRelated("Win32_USBController"))
            {
                foreach (ManagementObject obj in new ManagementObjectSearcher("ASSOCIATORS OF {Win32_USBController.DeviceID='" + controller["PNPDeviceID"].ToString() + "'}").Get())
                {
                    if(obj.ToString().Contains("DeviceID"))
                        USBobjects.Add(obj["DeviceID"].ToString());

                }
            }

        }

        int VidPidposition = USBobjects.IndexOf(Entity);
        for (int i = VidPidposition; i <= USBobjects.Count; i++ )
        {
            if (USBobjects[i].Contains("USBSTOR"))
            {
                Secondentity = USBobjects[i];
                break;
            }

        }
    }
Run Code Online (Sandbox Code Playgroud)

>

第二个功能:

    public void GetDriveLetter()
    {
        foreach (ManagementObject drive in new ManagementObjectSearcher("select * from Win32_DiskDrive").Get())
            {
                if (drive["PNPDeviceID"].ToString() == Secondentity)
                {
                    foreach (ManagementObject o in drive.GetRelated("Win32_DiskPartition"))
                    {
                        foreach (ManagementObject i in o.GetRelated("Win32_LogicalDisk"))
                        {
                            Console.WriteLine("Disk: " + i["Name"].ToString());
                        }
                    }
                }
        }
    }
Run Code Online (Sandbox Code Playgroud)

>