How to discover usb mass storage device (external hard drive) ?

256 Views Asked by At

I have tried these 2 different function but it doesn't work as expected. How to determine if drive is external drive <- the method provided on this also returns true for usb thumbdrive. But, i am looking specifically for external hard drive.

       DriveInfo[] allDrives = DriveInfo.GetDrives();
       foreach (DriveInfo d in allDrives)     
       if (d.DriveType == DriveType.Fixed && d.Name != "C:" + @"\"){}
1

There are 1 best solutions below

0
JazzmanJim On

The DriveType enum also has a Removable property:

System.IO.DriveType driveType = drive.DriveType;
switch (driveType)
{
    case System.IO.DriveType.CDRom:
        break;
    case System.IO.DriveType.Fixed:
        // Local Drive
        break;
    case System.IO.DriveType.Network:
        // Mapped Drive
        break;
    case System.IO.DriveType.NoRootDirectory:
        break;
    case System.IO.DriveType.Ram:
        break;
    case System.IO.DriveType.Removable:
        // Usually a USB Drive
        break;
    case System.IO.DriveType.Unknown:
        break;
}

You can query for the type.