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?
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?
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...
}
}
This can also, of course, be achieved using Linq:
IEnumerable<DriveInfo> readyDrives = Environment.GetLogicalDrives()
.Select(s => new DriveInfo(s))
.Where(di => di.IsReady);