Finding a USB mass storage devices "disk letter" by its PID and VID number or name


  • Hi

    I’m trying to write an application that will display how many bytes is free on a USB mass storage device. My problem is that the device “disk letter” (E:\ F:\ …) can change from time to time. I know my USB devices PID and VID number and I know what the device name is. Is it possible to find the device letter from this information and how can I do it?





    Friday, February 22, 2008 7:11 AM

All replies

  • Hi,

     By using System.Management you can get all the information you need about the USB storage devices including the serial number of the device to identify the device you want, this piece of code shows how to get these information about all devices.

    ManagementObjectSearcher diskDrives = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive WHERE InterfaceType='USB'");
                foreach (ManagementObject diskDrive in diskDrives.Get())
                    string DeviceID = diskDrive["DeviceID"].ToString();
                    string DriveLetter = "";
                    string DriveDescription = "";
                    // associate physical disks with partitions
                    ManagementObjectSearcher partitionSearcher = new ManagementObjectSearcher(String.Format(
                        "associators of {{Win32_DiskDrive.DeviceID='{0}'}} where AssocClass = Win32_DiskDriveToDiskPartition",
                    foreach (ManagementObject partition in partitionSearcher.Get())
                        // associate partitions with logical disks (drive letter volumes)
                        ManagementObjectSearcher logicalSearcher = new ManagementObjectSearcher(String.Format(
                            "associators of {{Win32_DiskPartition.DeviceID='{0}'}} where AssocClass = Win32_LogicalDiskToPartition",
                        foreach (ManagementObject logical in logicalSearcher.Get())
                            // finally find the logical disk entry to determine the volume name
                            ManagementObjectSearcher volumeSearcher = new ManagementObjectSearcher(String.Format(
                                "select * from Win32_LogicalDisk where Name='{0}'",
                            foreach (ManagementObject volume in volumeSearcher.Get())
                                DriveLetter = volume["Name"].ToString();
                                if (volume["VolumeName"] != null)
                                    DriveDescription = volume["VolumeName"].ToString();
                                char VolumeLetter = DriveLetter[0];
                                string VolumeName = DriveDescription;
                                string Manufacturer = (string)diskDrive["Manufacturer"];
                                string MediaType = (string)diskDrive["MediaType"];
                                string Model = (string)diskDrive["Model"];
                                string SerialNumber = (string)diskDrive["SerialNumber"];
                                long Size = Convert.ToInt64(volume["Size"]);
                                long FreeSpace = Convert.ToInt64(volume["FreeSpace"]);

    If this answer was helpful please remember to close your threads by marking helpful posts as answer


    Saturday, May 16, 2015 10:14 AM