0

I'm trying to find a particular USB device (1 or more) connected to my computer and retrieve the relevant path to the mounted drive. Ideally, it would be by finding the VID/PID of the USB device, but I'm not sure how to do that yet. The following works, but there must be some way to get the data in a single query.

What I'm doing here is looking or a physical drive that has a model matching HS SD Card Bridge USB Device and finding the physical drive # associated and using that to find the mounted partition..

        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("");
                }
            }
}

is there a way to do this from the VID/PID and a way to construct the search query so it requires just one query?

Kara
  • 6,115
  • 16
  • 50
  • 57
reza
  • 1,329
  • 2
  • 22
  • 37

1 Answers1

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;
        }
Zigma
  • 529
  • 6
  • 37