如何确定给定驱动器号是本地驱动器、映射驱动器还是 USB 驱动器?

发布于 2024-10-06 23:12:16 字数 72 浏览 0 评论 0原文

给定驱动器盘符,我如何确定它是什么类型的驱动器?

例如,E:\ 是 USB 驱动器、网络驱动器还是本地硬盘驱动器。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

泪之魂 2024-10-13 23:12:16

查看 DriveInfo< /a> 的 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;
    }
}

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;
    }
}
囚我心虐我身 2024-10-13 23:12:16

仅供其他人参考,这就是我将 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

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)

叶落知秋 2024-10-13 23:12:16

DriveInfo 还会将 USB HD 列为 DriveType.fixed,因此如果您需要知道驱动器的接口是否为 USB,这并没有帮助。下面是一个返回所有外部 USB 驱动器号的 VB.NET 函数:

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 任务:磁盘和文件系统

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

素衣风尘叹 2024-10-13 23:12:16

DriveType 将 SUBSTed 驱动器显示为 DriveType.Fixed

QueryDosDevice 可用于获取数据

   using System.Runtime.InteropServices;

   [DllImport("kernel32.dll", SetLastError=true)]
   static extern uint QueryDosDevice(string lpDeviceName, StringBuilder lpTargetPath, int ucchMax);

这是一个完整的解决方案:如何确定目录路径是否为 SUBST 'd

DriveType shows SUBSTed drives also as DriveType.Fixed.

QueryDosDevice can be used to get the data

   using System.Runtime.InteropServices;

   [DllImport("kernel32.dll", SetLastError=true)]
   static extern uint QueryDosDevice(string lpDeviceName, StringBuilder lpTargetPath, int ucchMax);

Here is a complete solution: How to determine if a directory path was SUBST'd.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文