Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 881
StubbornMule Avatar asked Sep 29 '08 13:09

StubbornMule


3 Answers

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);
    }
like image 112
Ray Hayes Avatar answered Nov 05 '22 22:11

Ray Hayes


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);
}
like image 35
GEOCHET Avatar answered Nov 06 '22 00:11

GEOCHET


Check System.IO.DriveInfo class and DriveType property.

like image 3
Biri Avatar answered Nov 05 '22 23:11

Biri