Querying Information for All Drives on a System
This code will help you to know if a drive (HDD, CD drive, DVD drive, etc.) is available and ready to be written to and/or read from. Additionally, helps you to know if you have enough available free space on the drive to write information to.
public static void Main(string args)
foreach (DriveInfo drive in DriveInfo.GetDrives())
Console.WriteLine("Drive " + drive.Name + " is ready.");
Console.WriteLine("AvailableFreeSpace: " + drive.AvailableFreeSpace);
Console.WriteLine("DriveFormat: " + drive.DriveFormat);
Console.WriteLine("DriveType: " + drive.DriveType);
Console.WriteLine("Name: " + drive.Name);
Console.WriteLine("RootDirectory.FullName: " +
Console.WriteLine("TotalFreeSpace: " + drive.TotalFreeSpace);
Console.WriteLine("TotalSize: " + drive.TotalSize);
Console.WriteLine("VolumeLabel: " + drive.VolumeLabel);
Console.WriteLine("Drive " + drive.Name + " is not ready.");
The IsReady property determines if the drive is ready to be queried, written to, or read from. The AvailableFreeSpace property returns the free space on that drive in bytes.
The thing you will notice is that the IsReady property is always tested for true before either using the drive or querying its properties. If we did not test this property for true and for some reason the drive was not ready (e.g., a CD was not in the drive at that time), a System.IO.IOException would be returned stating that "The device is not ready." To prevent this exception from being thrown (since it is an expensive operation), simply test the IsReady property to determine if it is true or not.
inthis code the static GeTDrives method of the DriveInfo class was used to return an array of DriveInfo objects. Each DriveInfo object in this array corresponds to one drive on the current system.
The DriveType property of the DriveInfo class returns an enumeration value from the DriveType enumeration (yes, they have the same name, unfortunately). This enumeration value identifies what type of drive the current DriveInfo object represents.
In the DriveInfo class there are two very similar properties, AvailableFreeSpace and TotalFreeSpace. Each of these properties will return the same value in most cases. However, AvailableFreeSpace also takes into account any disk-quota information for a particular drive. Disk-quota information can be found by right-clicking a drive in Windows Explorer and selecting the Properties pop-up menu item. This displays the Properties page for this drive. On this Properties page, click on the Quota tab to view the quota information for that drive. If the Enable Quota Management checkbox is unchecked, then disk-quota management is disabled, and both the AvailableFreeSpace and TotalFreeSpace properties should be equal.