How can I determine if a given drive letter is a local, mapped, or USB drive?

Benjol picture Benjol · Dec 9, 2010 · Viewed 21.3k times · Source

Given the letter of a drive, how can I determine what type of drive it is?

For example, whether E:\ is a USB drive, a network drive or a local hard drive.

Answer

djdd87 picture djdd87 · Dec 9, 2010

Have a look at DriveInfo's DriveType property.

System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();
foreach (var drive in drives)
{
    string driveName = drive.Name; // C:\, E:\, etc:\

    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;
    }
}