0

我正在尝试查找连接到我的计算机的特定 USB 设备(1 个或多个)并检索已安装驱动器的相关路径。理想情况下,这将是通过查找 USB 设备的 VID/PID,但我不知道如何做到这一点。以下方法有效,但必须有某种方法可以在单个查询中获取数据。

我在这里做的是寻找或具有模型匹配HS SD Card Bridge USB Device的物理驱动器并找到关联的物理驱动器#并使用它来查找已安装的分区..

        foreach (ManagementObject disk in disks.Get()) {
            //look for drives that match our string
            Match m = Regex.Match(disk["model"].ToString(), "HS SD Card Bridge USB Device");
            if (m.Success) {
                m = Regex.Match(disk["DeviceID"].ToString(), @"PHYSICALDRIVE(\d+)");
                if (m.Success) {
                    int driveNumber = Int32.Parse(m.Groups[1].ToString());
                    ManagementObjectSearcher mapping = new ManagementObjectSearcher("SELECT * FROM Win32_LogicalDiskToPartition");
                    foreach (ManagementObject map in mapping.Get()) {
                        m = Regex.Match(map["Antecedent"].ToString(), @"Disk #" + driveNumber + ",");
                        if (m.Success) {
                            string drive = map["Dependent"].ToString();
                            m = Regex.Match(drive, @"([A-Z]):");
                            if (m.Success) {
                                drive = m.Groups[1].ToString(); //< -- **FOUND**
                            }
                        }

                    }
                    //USBDevice dev = new USBDevice("", "");
                    //  list.Items.Add();
                    Console.WriteLine("");
                }
            }
}

有没有办法从 VID/PID 做到这一点,以及构造搜索查询的方法,所以它只需要一个查询?

4

1 回答 1

0

This is the one I used earlier . This will not be the answer. But will help you .

public int GetAvailableDisks()
        {
            int  deviceFound = 0;
            try
            {
                // browse all USB WMI physical disks
                foreach (ManagementObject drive in
                    new ManagementObjectSearcher(
                        "select DeviceID, Model from Win32_DiskDrive where InterfaceType='USB'").Get())
                {
                    ManagementObject partition = new ManagementObjectSearcher(String.Format(
                        "associators of {{Win32_DiskDrive.DeviceID='{0}'}} where AssocClass = Win32_DiskDriveToDiskPartition",
                        drive["DeviceID"])).First();
                    if (partition == null) continue;
                    // associate partitions with logical disks (drive letter volumes)
                    ManagementObject logical = new ManagementObjectSearcher(String.Format(
                        "associators of {{Win32_DiskPartition.DeviceID='{0}'}} where AssocClass = Win32_LogicalDiskToPartition",
                        partition["DeviceID"])).First();
                    if (logical != null)
                    {
                        // finally find the logical disk entry to determine the volume name - Not necesssary 
                        //ManagementObject volume = new ManagementObjectSearcher(String.Format(
                        //    "select FreeSpace, Size, VolumeName from Win32_LogicalDisk where Name='{0}'",
                        //    logical["Name"])).First();

                        string temp = logical["Name"].ToString() + "\\";
                        // +" " + volume["VolumeName"].ToString(); Future purpose if Device Name required

                        deviceFound++;
                        if (deviceFound > 1)
                        {
                            MessageBox.Show(@"Multiple Removeable media found. Please remove the another device");
                            deviceFound--;

                        }
                        else
                        {
                            driveName = temp;
                        }

                    }
                }
            }
            catch (Exception diskEnumerateException)
            {

            }
            return deviceFound;
        }
于 2013-08-22T06:25:53.707 回答