Monday, July 5, 2010

Determine Free Storage Space and Overall Size of a Drive

Listing all drives of the computer can be done like described in the post Enumerate Partitions, using the class DriveInfo. This classes provides also further information on the devices, as for example the size of the drive and the free space on it.
The following application lists all drives, iterates over them, looks for the given drive and outputs the space information then (System.IO has to be included):

DriveInfo[] Drives = DriveInfo.GetDrives(); // read all drives

foreach (DriveInfo d in Drives)
if (d.Name == "C:\\"// output size of partition C (replace by any)
Console.WriteLine((d.TotalFreeSpace / (1024 * 1024 * 1024)) + " GB of total " + (d.TotalSize / (1024 * 1024 * 1024)) + " GB available.");
The class DriveInfo has on the own hand the property TotalFreeSpace used here, and on the other hand AvailableFreeSpace. The difference is, that the first one saves the available free space in total, whereas the second one just saves the free space reservced for the current user.

No comments:

Post a Comment