Format Bytes to Megabytes or Kilobytes

by Erik Lane 23. June 2005 16:53

Code snippet that takes in a long (normally from FileInfo.Length) and returns a clean string for display.

private string FormatSize(long fileSize)
{
   if (fileSize < 1024)
   {
      return string.Format("{0:N0} B", fileSize);
   }
   else if (fileSize < 1024*1024)
   {
      return string.Format("{0:N0} KB", fileSize/1024);
   }
   else
   {
      return string.Format("{0:N1} MB", fileSize/(1024*1024));
   }
}
Tags:

Comments

Jimp
Jimp on 9/1/2005 7:24:00 AM

Thank you, that was useful

Comments are closed