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

标签 c# formatting filesize

有没有什么简单的方法可以创建一个使用 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());

希望这会有所帮助。

关于c# - 文件大小格式提供程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/128618/

相关文章:

c# - 这些示例是 C# 闭包吗?

c# - 使用 naudio 录制后,无效的 wav 文件未找到 fmt block

twig - 使用Twig模板系统可读取的文件大小

c# - 如何使用 StringBuilder (ASP.NET C#) 修改标签 <tbody>?

ruby-on-rails - 哪些工具可用于自动格式化 Ruby/Rails 代码

html - 随着屏幕分辨率/窗口大小的减小,固定图像和内容与固定侧边栏重叠

html - 如何让两个下拉列表并排显示?

javascript - Ajax - 下载前获取文件大小

windows - 单个 .msi Windows 程序包安装程序文件的最大大小是多少?

c# - 如何使用 Viewmodel-first 从代码隐藏设置 viewmodel 属性?