I have this program but I have an issue when this try to access the FIRST if... I need to separate the drivers and only get the total size of C:\. When the program stops at the if... say that the drive is not ready. What can I do?
class Program
{
static void Main(string[] args)
{
string mainHD = Path.GetPathRoot(Environment.GetFolderPath(Environment.SpecialFolder.System));
GetTotalFreeSpace(mainHD);
DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo drive in drives)
{
if (drive.VolumeLabel != @"C:\")
{
//There are more attributes you can use.
//Check the MSDN link for a complete example.
string drivesname = drive.Name;
Console.WriteLine(drivesname);
if (drive.IsReady) Console.WriteLine(drive.TotalSize);
}
}
Console.ReadLine();
}
private static long GetTotalFreeSpace(string driveName)
{
foreach (DriveInfo drive in DriveInfo.GetDrives())
{
if (drive.IsReady && drive.Name == driveName)
{
return drive.TotalSize;
}
}
return -1;
}
}
}
It looks like you should be testing drive.IsReady
before checking drive.VolumeLabel
.
Take a look at the MSDN sample.
Try this:
foreach (DriveInfo drive in drives)
{
if (drive.IsReady && drive.VolumeLabel != @"C:\")
{
//There are more attributes you can use.
//Check the MSDN link for a complete example.
string drivesname = drive.Name;
Console.WriteLine(drivesname);
Console.WriteLine(drive.TotalSize);
}
}
While this may be an issue of the C: drive not being ready, it could also be a case where there is an A: or B: floppy drive, and in that case, continuing to check IsReady
until it is ready is probably not a good idea.