22

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Benjol
  • 63,995
  • 54
  • 186
  • 268
  • 1
    "The title says it all" - no, it doesn;t. What language/OS are you on? Do you need to detect a USB drive being plugged in, etc etc – Jamiec Dec 09 '10 at 09:32
  • 3
    @Jamiec - I think in this case it did. Look at his tags - C#, means he's using C#.Net, .Net means he's using Windows (most probably), and his question title clearly states he wants to know what type of drive a drive letter is. Anyway, I edited the question. – djdd87 Dec 09 '10 at 09:36
  • 2
    @Jamiec, the 'detect USB drive plugged in' question has already been thrashed to death here on SO ;) – Benjol Dec 09 '10 at 09:44

5 Answers5

43

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;
    }
}
djdd87
  • 67,346
  • 27
  • 156
  • 195
10

Just for reference for anyone else, this is what I turned GenericTypeTea's answer into:

/// <summary>
/// Gets the drive type of the given path.
/// </summary>
/// <param name="path">The path.</param>
/// <returns>DriveType of path</returns>
public static DriveType GetPathDriveType(string path)
{
    //OK, so UNC paths aren't 'drives', but this is still handy
    if(path.StartsWith(@"\\")) return DriveType.Network;  
    var info = 
          DriveInfo.GetDrives()
          Where(i => path.StartsWith(i.Name, StringComparison.OrdinalIgnoreCase))
          FirstOrDefault();
    if(info == null) return DriveType.Unknown;
    return info.DriveType;
}

(You might want also take note of A.J.Bauer's answer: DriveInfo will also list USB HDs as DriveType.fixed)

Community
  • 1
  • 1
Benjol
  • 63,995
  • 54
  • 186
  • 268
  • Nice sample, but I would modify the line to `path.StartsWith(i.Name, StringComparison.OrdinalIgnoreCase)` so that it isn't case sensitive. – DeCaf Jun 26 '12 at 14:12
7

DriveInfo will also list USB HDs as DriveType.fixed, so this doesn't help if you need to know if a drive's interface is USB or not. Here is a VB.NET function that returns all external USB drive letters:

Imports System.Management

Public Shared Function GetExternalUSBDriveLettersCommaSeparated() As String
    Dim usbDrivesString As String = ""

    Dim wmiDiskDriveDeviceID As String = ""
    Dim wmiDiskDriveMediaType As String = ""
    Dim wmiDiskPartitionDeviceID As String = ""
    Dim wmiLogicalDiskDeviceID As String = ""

    Using wmiDiskDrives = New ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive WHERE InterfaceType='USB'")
        For Each wmiDiskDrive As ManagementObject In wmiDiskDrives.Get
            wmiDiskDriveDeviceID = wmiDiskDrive("DeviceID").ToString
            wmiDiskDriveMediaType = wmiDiskDrive("MediaType").ToString.ToLower
            If wmiDiskDriveMediaType.Contains("external") Then
                Using wmiDiskPartitions = New ManagementObjectSearcher("ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" + wmiDiskDriveDeviceID + "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition")
                    For Each wmiDiskPartition As ManagementObject In wmiDiskPartitions.Get
                        wmiDiskPartitionDeviceID = wmiDiskPartition("DeviceID").ToString
                        Using wmiLogicalDisks = New ManagementObjectSearcher("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" + wmiDiskPartitionDeviceID + "'} WHERE AssocClass = Win32_LogicalDiskToPartition")
                            For Each wmiLogicalDisk As ManagementObject In wmiLogicalDisks.Get
                                wmiLogicalDiskDeviceID = wmiLogicalDisk("DeviceID").ToString
                                If usbDrivesString = "" Then
                                    usbDrivesString = wmiLogicalDiskDeviceID
                                Else
                                    usbDrivesString += "," + wmiLogicalDiskDeviceID
                                End If
                            Next
                        End Using
                    Next
                End Using
            End If
        Next
    End Using

    Return usbDrivesString
End Function

See this MSDN link: WMI Tasks: Disks and File Systems

A.J.Bauer
  • 2,803
  • 1
  • 26
  • 35
1

DriveType shows SUBSTed drives also as DriveType.Fixed.

QueryDosDevice can be used to get the data

   using System.Runtime.InteropServices;

   [DllImport("kernel32.dll", SetLastError=true)]
   static extern uint QueryDosDevice(string lpDeviceName, StringBuilder lpTargetPath, int ucchMax);

Here is a complete solution: How to determine if a directory path was SUBST'd.

marsh-wiggle
  • 2,508
  • 3
  • 35
  • 52
0

Have a look at DriveInfo and DriveType

Adriaan Stander
  • 162,879
  • 31
  • 289
  • 284