Skip to content

Instantly share code, notes, and snippets.

@sebnilsson
Last active December 11, 2015 04:38
Show Gist options
  • Save sebnilsson/4546157 to your computer and use it in GitHub Desktop.
Save sebnilsson/4546157 to your computer and use it in GitHub Desktop.
Get human-readable file-sizes
public class FileSizeHelper
{
private static readonly string[] Units = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
private static string GetReadableFileSize(long size) // Size in bytes
{
int unitIndex = 0;
while (size >= 1024)
{
size /= 1024;
++unitIndex;
}
string unit = Units[unitIndex];
return string.Format("{0:0.#} {1}", size, unit);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment