tags:

views:

4750

answers:

3

I'm writing a setup program to install an application to 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 familiar with .NET the most I initially plan to try either custom .NET installer or setup component on .NET.

is it possible to determine drive letter of USB Flash drive on windows using .NET? how?

+11  A: 

You could use:

from driveInfo in DriveInfo.GetDrives()
where driveInfo.DriveType == DriveType.Removable
select driveInfo.RootDirectory.FullName

HTH, Kent

Kent Boogaart
+1  A: 

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

Geoffrey Chetwood
+6  A: 

c# 2.0 version of Kents 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.FullName);
    }
}
EricSchaefer