class Program
{
static void Main(string[] args)
{
DriveInfo a = new DriveInfo("a");
DriveInfo c = new DriveInfo("c");
WriteDriveInfo(a);
WriteDriveInfo(c);
}
private static void WriteDriveInfo(DriveInfo drive)
{
try
{
Console.WriteLine(string.Format("{0} ({1}): {2}", drive.DriveType, drive.DriveFormat, drive.VolumeLabel));
Console.WriteLine(string.Format("{0} / {1}", drive.TotalFreeSpace.Humanize(), drive.TotalSize.Humanize()));
}
catch (DriveNotFoundException dex)
{
Console.WriteLine(dex.Message);
}
}
}
public static class SizeExtensions
{
// Credits to http://blogs.interakting.co.uk/brad/archive/2008/01/24/C-Getting-a-user-friendly-file-size-as-a-string.aspx
public static string Humanize(this long bytes)
{
double s = bytes;
string[] format = new string[] { "{0} bytes", "{0} KB", "{0} MB", "{0} GB", "{0} TB", "{0} PB", "{0} EB", "{0} ZB", "{0} YB" };
int i = 0;
while (i < format.Length - 1 && s >= 1024)
{
s = (100 * s / 1024) / 100.0;
i++;
}
return string.Format(format[i], s.ToString("###,###,##0.##"));
}
}