Filesize Extension Method
post_id: 27 / post_date: 2009-10-13
This extension method humanizes the file size (FileInfo.Length is long but we need to make it into a decimal to get decimal places (long / long = long).
public static class Extensions { public static readonly long Kb = 1024; public static readonly long Mb = 1024 * 1024; public static readonly long Gb = 1024 * 1024 * 1024; public static string FileSize(this long length) { decimal size = Convert.ToDecimal(length); if (size > Gb) return string.Format("{0:#.##} Gb", size / Gb); else if (size > Mb) return string.Format("{0:#.##} Mb", size / Mb); else if (size > Kb) return string.Format("{0:#.##} Kb", size / Kb); else return string.Format("{0} b", size); } }