views:

1087

answers:

5

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

+3  A: 

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);
}
Geoffrey Chetwood
+3  A: 

Check System.IO.DriveInfo class and DriveType property.

Biri
+2  A: 
system.IO.DriveInfo.GetDrives(0).DriveType
Eduardo Campañó
Please don't downvote this one. All three answers came within a minute, so it's only coincidence.
Biri
@Biri: Don't tell people not to downvote, just upvote if you think the answer is good.
Geoffrey Chetwood
@Rich B: I didn't tell, I asked, which is a big difference. BTW I upvoted it of course.
Biri
@Biri: 'Asking' implies a question, which is not what your statement was. Upvoting this answer is a bit ridiculous IMO, since it is wrong.
Geoffrey Chetwood
@Rich B: Come on! Don't be so hard. He can also correct his one, as you also extended your one. We can also argue on the word 'ask', as English is not my native language and I was tought that this word can mean a request. And we can continue on :-P
Biri
@Biri: I am not here to argue, it is what it is.
Geoffrey Chetwood
+14  A: 

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);
    }
Ray Hayes
A: 

How can I detect the difference between a CD and a DVD drive, and the difference between a floppy drive and a USB? They have the same DriveType...