ManagmentObject查询windows(C#)以查找usb设备

本文关键字:查找 usb 设备 查询 windows ManagmentObject | 更新日期: 2023-09-27 18:28:19

我正在尝试查找连接到我的计算机的特定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中做到这一点,以及一种方法来构造搜索查询,使其只需要一个查询?

ManagmentObject查询windows(C#)以查找usb设备

这是我之前使用的一个。这不会是答案。但会帮助你的。

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;
        }