C#: File-size format provider

Posted by Seb Nilsson on Stack Overflow See other posts from Stack Overflow or by Seb Nilsson
Published on 2008-09-24T17:44:34Z Indexed on 2010/03/18 4:01 UTC
Read the original article Hit count: 572

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about c#

Related posts about formatting