给定驱动器号,如何确定驱动器类型?

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

最佳答案

看一看 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;
    }
}

07-27 17:33