wayne on April 4, 2008
Thank you Fat Angus for having a post like this available for all to use. I just happened to need a file size format routine, and Viola, Fat Angus has it!
I've put the code within the post to ensure I can still get to it later.
public class FileHelper
{
private static readonly long kilobyte = 1024;
private static readonly long megabyte = 1024 * kilobyte;
private static readonly long gigabyte = 1024 * megabyte;
private static readonly long terabyte = 1024 * gigabyte;
public static string ToByteString(long bytes)
{
if (bytes > terabyte)
return (bytes / terabyte).ToString("0.00 TB");
else if (bytes > gigabyte) return (bytes / gigabyte).ToString("0.00 GB");
else if (bytes > megabyte) return (bytes / megabyte).ToString("0.00 MB");
else if (bytes > kilobyte) return (bytes / kilobyte).ToString("0.00 KB");
else return bytes + " Bytes";
}
}
I will probably come back and expand on this later on when I have more time.
Fat Agnus » An example on how to format file size in C#