Feb 26, 2006 Earlier today, I posted a method that
returns the total size of a directory, including all subdirectories. It returns the
bytes and not kilobytes, megabytes or gigabytes, so I write a little formatting method
to do just that.
private string SizeFormat(float size, string formatString)
{
if (size
< 1024)
return size.ToString(formatString) + "bytes";
if (size
< Math.Pow(1024, 2))
return (size / 1024).ToString(formatString) + "
kb";
if (size
< Math.Pow(1024, 3))
return (size / Math.Pow(1024, 2)).ToString(formatString) + "
mb";
if (size
< Math.Pow(1024, 4))
return (size / Math.Pow(1024, 3)).ToString(formatString) + "
gb";
return size.ToString(formatString);
}
The method take a size and a formatString parameter. The formatString parameter is
for the formatting of the numbers themselves. You can format them to contain decimals
or separators or whatever you please. This is how to call the method so it returns
decimals:
SizeFormat(3210540, "N")
This method call returns this string: "3,14 MB"
* $4.95/month BlogEngine.net Hosting – Click Here!