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

本文关键字:驱动器 是否 硬盘驱动器 任何特 何检测 检测 | 更新日期: 2023-09-27 17:48:51

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

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

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);
    }

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属性。