How to detect if any specific drive is a hard drive?

给你一囗甜甜゛ 提交于 2019-11-26 23:03:26

问题


In C# how do you detect is a specific drive is a Hard Drive, Network Drive, CDRom, or floppy?


回答1:


The method GetDrives() returns a DriveInfo class which has a property DriveType that corresponds to the enumeration of 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.   
}

Here is a slightly adjusted example from MSDN that displays information for all drives:

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



回答2:


DriveInfo.DriveType should work for you.

DriveInfo[] allDrives = DriveInfo.GetDrives();

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



回答3:


Check System.IO.DriveInfo class and DriveType property.



来源:https://stackoverflow.com/questions/148742/how-to-detect-if-any-specific-drive-is-a-hard-drive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!