An example on how to format file size in C#

Thank you Fat Angus for having a post like this available for all to use.  I just happened to need a file size format routine, and Viola, Fat Angus has it!

I've put the code within the post to ensure I can still get to it later.

public class FileHelper
{
    private static readonly long kilobyte = 1024; 
    private static readonly long megabyte = 1024 * kilobyte; 
    private static readonly long gigabyte = 1024 * megabyte; 
    private static readonly long terabyte = 1024 * gigabyte;
    public static string ToByteString(long bytes)
    {
        if (bytes > terabyte)
            return (bytes / terabyte).ToString("0.00 TB");
        else if (bytes > gigabyte) return (bytes / gigabyte).ToString("0.00 GB");
        else if (bytes > megabyte) return (bytes / megabyte).ToString("0.00 MB"); 
        else if (bytes > kilobyte) return (bytes / kilobyte).ToString("0.00 KB"); 
        else return bytes + " Bytes";
    }
}
I will probably come back and expand on this later on when I have more time.

Fat Agnus » An example on how to format file size in C#

permalink Permalink or Trackback Comment Comments (0) Cat C#
Technorati: No reaction yet!
Tags: ,
Actions: E-mail

Can you dig it?

If you liked this or maybe found it helpful, please digg it, stumble it, buzz it, whatever it, to say thank you.



stumble

Add to Technorati Favorites

 
rss If you would like to receive these posts as they happen, you can subscribe to my feed or receive my posts in your email.

Related Posts

Add comment



(Will show your Gravatar icon)  

biuquote
  • Comment
  • Preview
Loading



Check it out mango: Any links must be entered as http://www.somewhere.com with nothing touching it. Anything else will be mangled. This is to help combat spam and to also ensure the masses know of this little tidbit before they click Save comment below. :) I have this down to remind me to do something with it, but I take things slow and easy on the old horse.

Keeps her regular don't ya know, and I wouldn't want to disturb that.



CSS Template by RamblingSoul | Illinois Wine. Adapted to BlogEngine by Wayne John
EatonWeb Blog Directory  Blog Directory LS Blogs Blog Directory Entrecard DropSurf
Powered By Ringsurf A1 Web Links