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

时间:2020-03-06 14:52:29  来源:igfitidea点击:

在Chow中,我们是否检测到特定驱动器是硬盘驱动器,网络驱动器,CDRom或者软盘?

解决方案

DriveInfo.DriveType应该适合我们。

DriveInfo[] allDrives = DriveInfo.GetDrives();

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

检查System.IO.DriveInfo类和DriveType属性。

方法GetDrives()返回一个DriveInfo类,该类具有一个属性DriveType,该属性对应于System.IO.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);
    }