• <tfoot id='C8rXV'></tfoot>

    <small id='C8rXV'></small><noframes id='C8rXV'>

    <i id='C8rXV'><tr id='C8rXV'><dt id='C8rXV'><q id='C8rXV'><span id='C8rXV'><b id='C8rXV'><form id='C8rXV'><ins id='C8rXV'></ins><ul id='C8rXV'></ul><sub id='C8rXV'></sub></form><legend id='C8rXV'></legend><bdo id='C8rXV'><pre id='C8rXV'><center id='C8rXV'></center></pre></bdo></b><th id='C8rXV'></th></span></q></dt></tr></i><div id='C8rXV'><tfoot id='C8rXV'></tfoot><dl id='C8rXV'><fieldset id='C8rXV'></fieldset></dl></div>
  • <legend id='C8rXV'><style id='C8rXV'><dir id='C8rXV'><q id='C8rXV'></q></dir></style></legend>

        • <bdo id='C8rXV'></bdo><ul id='C8rXV'></ul>
      1. 文件大小格式提供程序

        File-size format provider(文件大小格式提供程序)
          <bdo id='g92HL'></bdo><ul id='g92HL'></ul>

              <i id='g92HL'><tr id='g92HL'><dt id='g92HL'><q id='g92HL'><span id='g92HL'><b id='g92HL'><form id='g92HL'><ins id='g92HL'></ins><ul id='g92HL'></ul><sub id='g92HL'></sub></form><legend id='g92HL'></legend><bdo id='g92HL'><pre id='g92HL'><center id='g92HL'></center></pre></bdo></b><th id='g92HL'></th></span></q></dt></tr></i><div id='g92HL'><tfoot id='g92HL'></tfoot><dl id='g92HL'><fieldset id='g92HL'></fieldset></dl></div>

              <small id='g92HL'></small><noframes id='g92HL'>

                • <legend id='g92HL'><style id='g92HL'><dir id='g92HL'><q id='g92HL'></q></dir></style></legend>
                  <tfoot id='g92HL'></tfoot>

                    <tbody id='g92HL'></tbody>
                • 本文介绍了文件大小格式提供程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

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

                  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
                  otepad.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 字节"的字符串等等.

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

                  一个使用示例是:

                  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 类是密封的:(

                  There is a problem with ToString(), it's expecting a NumberFormatInfo type that implements IFormatProvider but the NumberFormatInfo class is sealed :(

                  如果你使用的是 C# 3.0,你可以使用扩展方法来获得你想要的结果:

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

                  你可以这样使用它.

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

                  希望这会有所帮助.

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

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Force JsonConvert.SerializeXmlNode to serialize node value as an Integer or a Boolean(强制 JsonConvert.SerializeXmlNode 将节点值序列化为整数或布尔值)
                  Using JSON to Serialize/Deserialize TimeSpan(使用 JSON 序列化/反序列化 TimeSpan)
                  Could not determine JSON object type for type quot;Classquot;(无法确定类型“Class的 JSON 对象类型.)
                  How to deserialize a JSONP response (preferably with JsonTextReader and not a string)?(如何反序列化 JSONP 响应(最好使用 JsonTextReader 而不是字符串)?)
                  how to de-serialize JSON data in which Timestamp it-self contains fields?(如何反序列化时间戳本身包含字段的JSON数据?)
                  JSON.Net custom contract serialization and Collections(JSON.Net 自定义合约序列化和集合)

                      <small id='OkLXp'></small><noframes id='OkLXp'>

                      <i id='OkLXp'><tr id='OkLXp'><dt id='OkLXp'><q id='OkLXp'><span id='OkLXp'><b id='OkLXp'><form id='OkLXp'><ins id='OkLXp'></ins><ul id='OkLXp'></ul><sub id='OkLXp'></sub></form><legend id='OkLXp'></legend><bdo id='OkLXp'><pre id='OkLXp'><center id='OkLXp'></center></pre></bdo></b><th id='OkLXp'></th></span></q></dt></tr></i><div id='OkLXp'><tfoot id='OkLXp'></tfoot><dl id='OkLXp'><fieldset id='OkLXp'></fieldset></dl></div>
                        <bdo id='OkLXp'></bdo><ul id='OkLXp'></ul>

                            <tbody id='OkLXp'></tbody>
                        • <tfoot id='OkLXp'></tfoot>

                            <legend id='OkLXp'><style id='OkLXp'><dir id='OkLXp'><q id='OkLXp'></q></dir></style></legend>