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.
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