c# - 文件大小格式提供程序

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

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...
}

它应该产生格式为“2,5 MB”、“3,9 GB”、“670 字节”的字符串等等。

最佳答案

我用的是这个,我从网上得到的

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();
    }

}

一个使用示例是:

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

http://flimflan.com/blog/FileSizeFormatProvider.aspx 的积分

ToString() 有问题,它需要一个实现 IFormatProvider 的 NumberFormatInfo 类型,但 NumberFormatInfo 类是密封的:(

如果你使用的是 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());

希望这会有所帮助。

https://stackoverflow.com/questions/128618/

相关文章:

visual-studio-2010 - 我可以让 Visual Studio 将花括号与 if 语

angularjs - 删除 AngularJS 货币过滤器小数/美分

intellij-idea - 如何在 WebStorm 中自动格式化代码?

xml - 在 Eclipse 中格式化 XML 代码

.net - .NET 中是否有一种简单的方法来获取数字的 "st"、 "nd"、 "rd"和 "t

java - 如何将 SimpleDateFormat 与日历一起使用?

c++ - 我应该在我的 C++ 代码中使用 printf 吗?

c++ - 如何使用 ostream 在 c++ 中将 unsigned char 打印为十六进制?

visual-studio - 如何在 Visual Studio (Express) 2008 中

c# - 如何使 Visual Studio 不将 { 放在新行上?