filesize extension method

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);
			return string.Format("{0} b", size);


Warning: count(): Parameter must be an array or an object that implements Countable in /home/cselian/public_html/blog/wp-includes/class-wp-comment-query.php on line 405

Leave a Comment