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 realize now that you were actually asking for something that would work with String.Format() - I guess I should have read the question twice before posting ;-)
I don't like the solution where you have to explicitly pass in a format provider every time - from what I could gather from this article, the best way to approach this, is to implement a FileSize type, implementing the IFormattable interface.
I went ahead and implemented a struct that supports this interface, and which can be cast from an integer. In my own file-related APIs, I will have my .FileSize properties return a FileSize instance.
Here's the code:
And a simple Unit Test that demonstrates how this works:
As you can see, the FileSize type can now be formatted correctly, and it is also possible to specify the number of decimals, as well as applying regular numeric formatting if required.
I guess you could take this much further, for example allowing explicit format selection, e.g. "{0:KB}" to force formatting in kilobytes. But I'm going to leave it at this.
I'm also leaving my initial post below for those two prefer not to use the formatting API...
100 ways to skin a cat, but here's my approach - adding an extension method to the int type:
With this extension in your assembly, to format a filesize, simply use a statement like (1234567).ToBytes()
The following MbUnit test clarifies precisely what the output looks like:
And you can easily change the units and precision to whatever suits your needs :-)
A Domain Driven Approach can be found here: https://github.com/Corniel/Qowaiv/blob/master/src/Qowaiv/IO/StreamSize.cs
The StreamSize struct is a representation of a stream size, allows you both to format automatic with the proper extension, but also to specify that you want it in KB/MB or whatever. This has a lot of advantages, not only because you get the formatting out of the box, it also helps you to make better models, as it is obvious than, that the property or the result of a method represents a stream size. It also has an extension on file size: GetStreamSize(this FileInfo file).
Short notation
Full notation
Custom
There is a NuGet-package, so you just can use that one: https://www.nuget.org/packages/Qowaiv
this is the simplest implementation I know to format file sizes:
Whereas Size is the unformatted file size in bytes.
Greetings Christian
http://www.wpftutorial.net
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.
Hope this helps.
since shifting is a very cheap operation
My code... thanks for Shaun Austin.