C#: File-size format provider
- by Seb Nilsson
Is there any easy way to create a class that uses IFormatProvider that writes out a user-friendly file-size?
public static string GetFileSizeString(string filePath)
{
FileInfo info = new FileInfo(@"c:\windows\notepad.exe");
long size = info.Length;
string sizeString = size.ToString(FileSizeFormatProvider); // This is where the class does its magic...
}
It should result in strings formatted something like "2,5 MB", "3,9 GB", "670 bytes" and so on.