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
2008-09-29 13:57:33
Please don't downvote this one. All three answers came within a minute, so it's only coincidence.
Biri
2008-09-29 14:00:12
@Biri: Don't tell people not to downvote, just upvote if you think the answer is good.
Geoffrey Chetwood
2008-09-29 14:02:27
@Rich B: I didn't tell, I asked, which is a big difference. BTW I upvoted it of course.
Biri
2008-09-29 14:06:48
@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
2008-09-29 14:08:58
@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
2008-09-29 14:13:52
@Biri: I am not here to argue, it is what it is.
Geoffrey Chetwood
2008-09-29 14:22:34
+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
2008-09-29 14:09:34