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

有帮助吗?

解决方案

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

其他提示

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top