考虑到驱动器的字母,我该如何确定驱动器的类型?
例如,E:\是USB驱动器,网络驱动器还是本地硬盘驱动器。
答案 0 :(得分:34)
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;
}
}
答案 1 :(得分:7)
仅供其他人参考,这就是我将GenericTypeTea的答案转换为:
/// <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;
}
(您可能还需要注意A.J.Bauer的answer: DriveInfo也会将USB HD列为DriveType.fixed )
答案 2 :(得分:4)
DriveInfo还会将USB HD列为DriveType.fixed,因此如果您需要知道驱动器的接口是否为USB,这无济于事。这是一个VB.NET函数,它返回所有外部USB驱动器号:
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
请参阅此MSDN链接: WMI Tasks: Disks and File Systems
答案 3 :(得分:2)
答案 4 :(得分:0)
DriveType还会将SUBSTed个驱动器显示为DriveType.Fixed
。
要检查驱动器是否已被补充,我使用代码How to determine if a directory path was SUBST'd。