How to find USB drive letter?
Asked Answered
F

3

14

I'm writing a setup program to install an application to a USB drive. The application is meant to be used only from USB drives, so it would save an extra step for the user by automatically selecting USB drive to install to.

I might explore using Nullsoft or MSI for install, but since I'm mostly familiar with .NET I initially plan to try either custom .NET installer or setup component on .NET.

Is it possible to determine the drive letter of a USB flash drive on Windows using .NET? How?

Ferous answered 23/9, 2008 at 21:15 Comment(1)
Is the program itself running from a Flash drive? Or are you just trying to say "hey, here's the drives on the system, here's which ones are Flash drives" ?Jibe
P
18

You could use:

from driveInfo in DriveInfo.GetDrives()
where driveInfo.DriveType == DriveType.Removable && driveInfo.IsReady
select driveInfo.RootDirectory.FullName
Pharyngeal answered 23/9, 2008 at 21:18 Comment(3)
Also gets FireWire drives, but mthat ight actually be the right thing to do though.Conjunctivitis
But doesn't return USB hard drives, I think it is a problem.Finny
A USB hard drive is not exactly a "USB flash drive", or is it? ;-)Amaya
W
15

This will enumerate all the drives on the system without LINQ but still using WMI:

// browse all USB WMI physical disks

foreach(ManagementObject drive in new ManagementObjectSearcher(
    "select * from Win32_DiskDrive where InterfaceType='USB'").Get())
{
    // associate physical disks with partitions

    foreach(ManagementObject partition in new ManagementObjectSearcher(
        "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" + drive["DeviceID"]
          + "'} WHERE AssocClass = 
                Win32_DiskDriveToDiskPartition").Get())
    {
        Console.WriteLine("Partition=" + partition["Name"]);

        // associate partitions with logical disks (drive letter volumes)

        foreach(ManagementObject disk in new ManagementObjectSearcher(
            "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='"
              + partition["DeviceID"]
              + "'} WHERE AssocClass =
                Win32_LogicalDiskToPartition").Get())
        {
            Console.WriteLine("Disk=" + disk["Name"]);
        }
    }

    // this may display nothing if the physical disk

    // does not have a hardware serial number

    Console.WriteLine("Serial="
     + new ManagementObject("Win32_PhysicalMedia.Tag='"
     + drive["DeviceID"] + "'")["SerialNumber"]);
}

Source

Waddle answered 23/9, 2008 at 21:29 Comment(2)
ManagementObject, ManagementObjectSearcher and ManagementObjectCollection (implicitly retrieved with .Get()) have a Dispose method that should be called.Pyridine
I used your solution to find the drive letter of one single flash drive of which I knew the serial number. It worked well under Windows 10 but in Windows 7 the serial number was empty. I figured out that the serial number is also part of drive["PNPDeviceID"] which I now test if it contains the serial number I was looking for. Thanks!Hock
A
15

C# 2.0 version of Kent's code (from the top of my head, not tested):

IList<String> fullNames = new List<String>();
foreach (DriveInfo driveInfo in DriveInfo.GetDrives()) {
    if (driveInfo.DriveType == DriveType.Removable) {
        fullNames.Add(driveInfo.RootDirectory.FullName);
    }
}
Amaya answered 23/9, 2008 at 21:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.