文件大小的格式提供者 [英] File-size format provider

查看:167
本文介绍了文件大小的格式提供者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有简单的方法来创建一个使用 IFormatProvider 的类来写出一个用户友好的文件大小?

  public static string GetFileSizeString(string filePath)
{
FileInfo info = new FileInfo(@c:\windows\\\
otepad.exe);
long size = info.Length;
string sizeString = size.ToString(FileSizeFormatProvider); //这是类魔法的地方...
}

格式化为 2,5 MB 3,9 GB 670字节等字符串。
$ div b











$ b $ (formatType == typeof(ICustomFormatter))public class FileSizeFormatProvider:IFormatProvider,ICustomFormatter
{
public object GetFormat(Type formatType)
{
if(formatType == typeof返回这个;
返回null;
}

private const string fileSizeFormat =fs;
private const十进制OneKiloByte = 1024M;
私人常量十进制OneMegaByte = OneKiloByte * 1024M;
private const Decimal OneGigaByte = OneMegaByte * 1024M;
$ b公共字符串格式(字符串格式,对象arg,IFormatProvider formatProvider)
{
if(format == null ||!format.StartsWith(fileSizeFormat))
{
返回defaultFormat(format,arg,formatProvider);

$ b $ if(arg is string)
{
return defaultFormat(format,arg,formatProvider);
}

十进制大小;

尝试
{
size = Convert.ToDecimal(arg);

catch(InvalidCastException)
{
return defaultFormat(format,arg,formatProvider);
}

字符串后缀;
if(size> OneGigaByte)
{
size / = OneGigaByte;
后缀=GB;
}
else if(size> OneMegaByte)
{
size / = OneMegaByte;
后缀=MB;
}
else if(size> OneKiloByte)
{
size / = OneKiloByte;
后缀=kB;
}
else
{
suffix =B;
}

string precision = format.Substring(2);
if(String.IsNullOrEmpty(precision))precision =2;
return String.Format({0:N+ precision +} {1},size,suffix);


$ b private static string defaultFormat(string format,object arg,IFormatProvider formatProvider)
{
IFormattable formattableArg = arg as IFormattable;
if(formattableArg!= null)
{
return formattableArg.ToString(format,formatProvider);
}
return arg.ToString();


$ b $ / code $ / pre

使用的一个例子是: Console.WriteLine(String.Format(new FileSizeFormatProvider(),文件大小:{0:fs}),100) );
Console.WriteLine(String.Format(new FileSizeFormatProvider(),File size:{0:fs},10000));



如果您使用的是C#3.0,则可以使用扩展方法来获得所需的结果:

  public static class ExtensionMethods 
{
public static string ToFileSize(this long l)
{
return String.Format(new FileSizeFormatProvider(),{0:fs},l);
}
}

您可以像这样使用它。

  long l = 100000000; 
Console.WriteLine(l.ToFileSize());

希望这有帮助。 >

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

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 is 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 as IFormattable;
        if (formattableArg != null)
        {
            return formattableArg.ToString(format, formatProvider);
        }
        return arg.ToString();
    }

}

an example of use would be:

Console.WriteLine(String.Format(new FileSizeFormatProvider(), "File size: {0:fs}", 100));
Console.WriteLine(String.Format(new FileSizeFormatProvider(), "File size: {0:fs}", 10000));

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:

public static class ExtensionMethods
{
    public static string ToFileSize(this long l)
    {
        return String.Format(new FileSizeFormatProvider(), "{0:fs}", l);
    }
}

You can use it like this.

long l = 100000000;
Console.WriteLine(l.ToFileSize());

Hope this helps.

这篇关于文件大小的格式提供者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆