如何检测特定驱动器是否为硬盘?

13

在C#中,如何检测特定驱动器是硬盘驱动器,网络驱动器,CDROM还是软盘?

3个回答

18

GetDrives()方法返回一个DriveInfo类,该类具有对应于System.IO.DriveType枚举的DriveType属性:

public enum DriveType
{
    Unknown,         // The type of drive is unknown.  
    NoRootDirectory, // The drive does not have a root directory.  
    Removable,       // The drive is a removable storage device, 
                     //    such as a floppy disk drive or a USB flash drive.  
    Fixed,           // The drive is a fixed disk.  
    Network,         // The drive is a network drive.  
    CDRom,           // The drive is an optical disc device, such as a CD 
                     // or DVD-ROM.  
    Ram              // The drive is a RAM disk.   
}

这里是来自MSDN的稍作调整的示例,显示所有驱动器的信息:

    DriveInfo[] allDrives = DriveInfo.GetDrives();
    foreach (DriveInfo d in allDrives)
    {
        Console.WriteLine("Drive {0}, Type {1}", d.Name, d.DriveType);
    }

FYI DriveType 返回外部 USB 硬盘的 DriveType.Fixed - Karel Frajták

4

DriveInfo.DriveType 可以帮助你。

DriveInfo[] allDrives = DriveInfo.GetDrives();

foreach (DriveInfo d in allDrives)
{
    Console.WriteLine("Drive {0}", d.Name);
    Console.WriteLine("  File type: {0}", d.DriveType);
}

3

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接