c# - File-size format provider -


is there easy way create class uses iformatprovider writes out 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); // class magic... } 

it should result in strings formatted "2,5 mb", "3,9 gb", "670 bytes" , on.

i use one, web

public class filesizeformatprovider : iformatprovider, icustomformatter {     public object getformat(type formattype)     {         if (formattype == typeof(icustomformatter)) return this;         return null;     }      private const string filesizeformat = "fs";     private const decimal onekilobyte = 1024m;     private const decimal onemegabyte = onekilobyte * 1024m;     private const decimal onegigabyte = onemegabyte * 1024m;      public string format(string format, object arg, iformatprovider formatprovider)     {             if (format == null || !format.startswith(filesizeformat))             {                 return defaultformat(format, arg, formatprovider);             }          if (arg string)             {                 return defaultformat(format, arg, formatprovider);             }          decimal size;          try             {                 size = convert.todecimal(arg);             }             catch (invalidcastexception)             {                 return defaultformat(format, arg, formatprovider);             }          string suffix;         if (size > onegigabyte)         {             size /= onegigabyte;             suffix = "gb";         }         else if (size > onemegabyte)         {             size /= onemegabyte;             suffix = "mb";         }         else if (size > onekilobyte)         {             size /= onekilobyte;             suffix = "kb";         }         else         {             suffix = " b";         }          string precision = format.substring(2);         if (string.isnullorempty(precision)) precision = "2";         return string.format("{0:n" + precision + "}{1}", size, suffix);      }      private static string defaultformat(string format, object arg, iformatprovider formatprovider)     {         iformattable formattablearg = arg iformattable;         if (formattablearg != null)         {             return formattablearg.tostring(format, formatprovider);         }         return arg.tostring();     }  } 

an example of use be:

console.writeline(string.format(new filesizeformatprovider(), "file size: {0:fs}", 100)); console.writeline(string.format(new filesizeformatprovider(), "file size: {0:fs}", 10000)); 

credits http://flimflan.com/blog/filesizeformatprovider.aspx

there problem tostring(), it's expecting numberformatinfo type implements iformatprovider numberformatinfo class sealed :(

if you're using c# 3.0 can use extension method result want:

public static class extensionmethods {     public static string tofilesize(this long l)     {         return string.format(new filesizeformatprovider(), "{0:fs}", l);     } } 

you can use this.

long l = 100000000; console.writeline(l.tofilesize()); 

hope helps.


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -