I've been looking for a way to find the drive letter assigned by windows to my device which implements a USB MSD + HID composite class. The most promising solution I've come across was
http://stackoverflow.com/questions/17371578/find-usb-drive-letter-from-vid-pid-needed-for-xp-and-higher on stackoverflow. But that does not quite work for me. (The PNPDeviceID returned by the Query on Win32_DiskDrive and that returned by the "Device" class are different.)
Finally, I settled on an indirect way by finding the Drive letter from a "Model" query on Win32_DiskDrive which happens to be the string in the "Product ID" field from the InquiryResponse of the MSD. Like this:
foreach (ManagementObject drive in new ManagementObjectSearcher("SELECT DeviceID FROM Win32_DiskDrive WHERE Model LIKE" + "\"%" + model + "%\"").Get())
{
// associate physical disks with partitions
ManagementObject partition = new ManagementObjectSearcher(System.String.Format("associators of {{Win32_DiskDrive.DeviceID='{0}'}} where AssocClass = Win32_DiskDriveToDiskPartition", drive["DeviceID"])).First();
if (partition != null)
{
// associate partitions with logical disks (drive letter volumes)
ManagementObject logical = new ManagementObjectSearcher(System.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
ManagementObject volume = new ManagementObjectSearcher(System.String.Format("select Name from Win32_LogicalDisk where Name='{0}'", logical["Name"])).First(); // FreeSpace, Size,
foreach (PropertyData usb in volume.Properties)
{
if (usb.Value != null && usb.Value.ToString() != "")
{
Debug.Write("WMI Search Successful! The Drive Letter is: ");
Debug.Write(usb.Value + "\r\n");
drive_path = usb.Value.ToString();
}
}
}
}
Was wondering if any one here knows of a more direct way of getting the drive letter from the VID/PID ?