[英]How can I determine if a given drive letter is a local, mapped, or USB drive?
鑒於驅動器的字母,我如何確定它是什么類型的驅動器?
例如,E:\\是USB驅動器,網絡驅動器還是本地硬盤驅動器。
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;
}
}
僅供其他人參考,這就是我將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;
}
(您可能還需要注意AJBauer的答案 : DriveInfo還會將USB HD列為DriveType.fixed )
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任務:磁盤和文件系統
DriveType 還將SUBSTed驅動器顯示為DriveType.Fixed
。
要檢查驅動器是否已被替換,我使用代碼如何確定目錄路徑是否為SUBST 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.