File Size Format

Have you ever wanted to display the size of the file in bytes, kilo bytes or mega bytes, here is a simple code snippet to do that.

private string FormatSize (double fileSize)
{
if (fileSize < 1024)
return String.Format(“{0:N0} B”, fileSize);
else if (fileSize < 1024*1024)
return String.Format(“{0:N2} KB”, fileSize/1024);
else
return String.Format(“{0:N2} MB”, fileSize/(1024*1024));
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s