How do I get a human-readable file size in bytes abbreviation using .NET?
Example: Take input 7,326,629 and display 6.98 MB
format("The size of the file: %d bytes", fileSize); These methods will output the size in Bytes. So to get the MB size, you need to divide the file size from (1024*1024).
Locate the file or folder whose size you would like to view. Click the file or folder. Press Command + I on your keyboard. A window opens and shows the size of the file or folder.
This may not the most efficient or optimized way to do it, but it's easier to read if you are not familiar with log maths, and should be fast enough for most scenarios.
string[] sizes = { "B", "KB", "MB", "GB", "TB" }; double len = new FileInfo(filename).Length; int order = 0; while (len >= 1024 && order < sizes.Length - 1) { order++; len = len/1024; } // Adjust the format string to your preferences. For example "{0:0.#}{1}" would // show a single decimal place, and no space. string result = String.Format("{0:0.##} {1}", len, sizes[order]);
using Log to solve the problem....
static String BytesToString(long byteCount) { string[] suf = { "B", "KB", "MB", "GB", "TB", "PB", "EB" }; //Longs run out around EB if (byteCount == 0) return "0" + suf[0]; long bytes = Math.Abs(byteCount); int place = Convert.ToInt32(Math.Floor(Math.Log(bytes, 1024))); double num = Math.Round(bytes / Math.Pow(1024, place), 1); return (Math.Sign(byteCount) * num).ToString() + suf[place]; }
Also in C#, but should be a snap to convert. Also I rounded to 1 decimal place for readability.
Basically determine the number of decimal places in Base 1024 and then divide by 1024^decimalplaces
.
And some samples of use and output:
Console.WriteLine(BytesToString(9223372036854775807)); //Results in 8EB Console.WriteLine(BytesToString(0)); //Results in 0B Console.WriteLine(BytesToString(1024)); //Results in 1KB Console.WriteLine(BytesToString(2000000)); //Results in 1.9MB Console.WriteLine(BytesToString(-9023372036854775807)); //Results in -7.8EB
Edit:
Was pointed out that I missed a Math.Floor
, so I incorporated it. (Convert.ToInt32
uses rounding, not truncating and that's why Floor
is necessary.) Thanks for the catch.
Edit2:
There were a couple of comments about negative sizes and 0 byte sizes, so I updated to handle those cases.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With