Friday, May 1, 2009

Convert File Size To Readable Format


Scenario
When we use "Windows + E" to browse files, we will see the file size of each file under the "Details" mode. Every file has a file size, but by default all file sizes are displayed in one unit 'KB'. This makes people difficult to make sense if the file size is greater than 10MB, because you will see a lot of numeric digits before the dot symbol.

Method
The following table is not magic and the issue mentioned above is not hard to solve. Here is a way to convert the file size into human readable format under normal circumstance.

1 KB = 1024 Byte = 1024 Byte
1 MB = 1024 KB = 1024 * 1024 Byte = 1048576 Byte
1 GB = 1024 MB = 1024 * 1024 KB = 1024 * 1024 * 1024 Byte = 1073741824 Byte


To convert the file size you retrieve from the original "File.Length" method, we can use the above table to check the byte count.

Code

Conclusion
As you see, after calling the "GetFileSize" method, it will return a converted format of file size string. The implementation is only a few if statements.
You can copy & modify the above code for your convenience.

comments

0 Responses to "Convert File Size To Readable Format"

Post a Comment

BlogUpp!

 

Copyright 2009 All Rights Reserved Revolution Two Lifestyle theme by Brian Gardner | Blogger template converted & enhanced by eBlog Templates