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.
On a Windows 10 computer, click the start menu, then click File Explorer, then click This PC. You will see a list of local disks and network connections. All other drive letters up to the letter “L” are local.
Drive mapping is how operating systems, such as Microsoft Windows, associate a local drive letter (A through Z) with a shared storage area to another computer (often referred as a File Server) over a network.
To do so, select and hold (or right-click) the Start button, and then select Disk Management. In Disk Management, select and hold (or right-click) the volume for which you want to change or add a drive letter, and then select Change Drive Letter and Paths.
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;
}
}
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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With