Human-readable file size in C#

static string ReadableFileSize(double size, int unit = 0)
{
    string[] units = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB" };

    while (size >= 1024)
    {
        size /= 1024;
        ++unit;
    }

    return String.Format("{0:G4} {1}", size, units[unit]);
}

I made “bytes” a double so you could pass in fractions, which you might get when calculating download speeds.