[英]How can I determine if a given drive letter is a local, mapped, or USB drive?
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. 例如,E:\\是USB驱动器,网络驱动器还是本地硬盘驱动器。
Have a look at DriveInfo
's DriveType
property. 看看
DriveInfo
的DriveType
属性。
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: 仅供其他人参考,这就是我将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;
}
(You might want also take note of AJBauer's answer : DriveInfo will also list USB HDs as DriveType.fixed ) (您可能还需要注意AJBauer的答案 : DriveInfo还会将USB HD列为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. DriveInfo还会将USB HD列为DriveType.fixed,因此如果您需要知道驱动器的接口是否为USB,这无济于事。 Here is a VB.NET function that returns all external USB drive letters:
这是一个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
See this MSDN link: WMI Tasks: Disks and File Systems 请参阅此MSDN链接: WMI任务:磁盘和文件系统
DriveType shows SUBSTed drives also as DriveType.Fixed
. DriveType 还将SUBSTed驱动器显示为
DriveType.Fixed
。
To check if a drive is substed I use the code How to determine if a directory path was SUBST'd . 要检查驱动器是否已被替换,我使用代码如何确定目录路径是否为SUBST 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.