views:

95

answers:

2

I use following code to get logical drives:

string[] strDrives = Environment.GetLogicalDrives();

but when I want to iterate through it, an exception occurs, with the message:

Drive Not Ready

How can I get just ready drives?

+7  A: 

Use DriveInfo to determine if the drive is ready.

foreach (var oneDrive in strDrives)
{
    var drive = new DriveInfo(oneDrive)
    if (drive.IsReady) 
    {
       // Do something with the drive...
    }
}
Kris Erickson
+1 ah- that's the much nicer solution than mine!
marc_s
+1  A: 

This can also, of course, be achieved using Linq:

IEnumerable<DriveInfo> readyDrives = Environment.GetLogicalDrives()
    .Select(s => new DriveInfo(s))
    .Where(di => di.IsReady);
Fredrik Mörk