Somacon.com: Articles on web development, software, and hardware
§ Home > Index > C and C# Programming

C# Human Readable File Size Optimized Function

This is a highly optimized function to get the file size abbreviation in bytes for an arbitrary, 64-bit file size. It returns the file size to three decimals followed by the abbreviation, like KB, MB, GB, etc. Use this function to display a human-readable, user-friendly file size to your users.

Below is the human readable file size function source code. You can get a Visual Basic (VB.Net) version by using the C# to VB.Net Convertor Tool.

C# Human-Readable File Size Function

// Returns the human-readable file size for an arbitrary, 64-bit file size
//  The default format is "0.### XB", e.g. "4.2 KB" or "1.434 GB"
public static string GetSizeReadable(long i)
{
    string sign = (i < 0 ? "-" : "");
    double readable = (i < 0 ? -i : i);
    string suffix;
    if (i >= 0x1000000000000000) // Exabyte
    {
        suffix = "EB";
        readable = (double)(i >> 50);
    }
    else if (i >= 0x4000000000000) // Petabyte
    {
        suffix = "PB";
        readable = (double)(i >> 40);
    }
    else if (i >= 0x10000000000) // Terabyte
    {
        suffix = "TB";
        readable = (double)(i >> 30);
    }
    else if (i >= 0x40000000) // Gigabyte
    {
        suffix = "GB";
        readable = (double)(i >> 20);
    }
    else if (i >= 0x100000) // Megabyte
    {
        suffix = "MB";
        readable = (double)(i >> 10);
    }
    else if (i >= 0x400) // Kilobyte
    {
        suffix = "KB";
        readable = (double)i;
    }
    else
    {
        return i.ToString(sign + "0 B"); // Byte
    }
    readable = readable / 1024;

    return sign + readable.ToString("0.### ") + suffix;
}

Sample Usage

It is recommended to put the above function in a helper or utility class as a public static method.

// EXAMPLE OUTPUT
GetSizeReadable(1023); // 1023 B
GetSizeReadable(1024); // 1 KB
GetSizeReadable(1025); // 1.001 KB

// Example of getting a file size and converting it to a readable value
string fileName = "abc.txt";
long fileSize = new System.IO.FileInfo(fileName).Length;
string sizeReadable = GetSizeReadable(fileSize);

Discussion

I wrote this function after being sorely disappointed by the versions posted on StackOverflow, at How do I get a human-readable file size abbreviation using .NET? Those versions had obvious performance issues.

This is not a function likely to be called very frequently in any individual program, so it may not be important to your project to shave some microseconds off this function call. However, I'm posting this here because if you add up the number of times it is called all over the world, there is a great potential savings of CPU-cycles if everyone were to use an optimized version.

This version is about two times faster and more efficient overall than David Thibault's version, the top-rated post on StackOverflow as of this writing. (If you take out the ToString, the most expensive function, the math portion is over 10 times faster.) Here are the key optimizations and features versus other versions posted there.

I'm just an average C# programmer with some experience in C++ optimization. If you find other ways to optimize this function, please let me know!


Created 2012-06-25, Last Modified 2013-07-18, © Shailesh N. Humbad
Disclaimer: This content is provided as-is. The information may be incorrect.