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.
I use this one, I get it from the web
an example of use would be:
Credits for http://flimflan.com/blog/FileSizeFormatProvider.aspx
There is a problem with ToString(), it’s expecting a NumberFormatInfo type that implements IFormatProvider but the NumberFormatInfo class is sealed 🙁
If you’re using C# 3.0 you can use an extension method to get the result you want:
You can use it like this.