【问题标题】:Extend File Class扩展文件类
【发布时间】:2011-07-15 18:45:57
【问题描述】:

是否可以扩展文件类?我想将新的 GetFileSize 方法添加到文件类并像这样使用它

string s = File.GetFileSize("c:\MyFile.txt");

实施

public static string GetFileSize(string fileName)
{

    FileInfo fi = new FileInfo(fileName);
    long Bytes = fi.Length;

    if (Bytes >= 1073741824)
    {
        Decimal size = Decimal.Divide(Bytes, 1073741824);
        return String.Format("{0:##.##} GB", size);
    }
    else if (Bytes >= 1048576)
    {
        Decimal size = Decimal.Divide(Bytes, 1048576);
        return String.Format("{0:##.##} MB", size);
    }
    else if (Bytes >= 1024)
    {
        Decimal size = Decimal.Divide(Bytes, 1024);
        return String.Format("{0:##.##} KB", size);
    }
    else if (Bytes > 0 & Bytes < 1024)
    {
        Decimal size = Bytes;
        return String.Format("{0:##.##} Bytes", size);
    }
    else
    {
        return "0 Bytes";
    }
}

我尝试使用扩展方法将方法添加到文件类,但编译器给出错误“'System.IO.File':静态类型不能用作参数”

【问题讨论】:

    标签: c# class extend


    【解决方案1】:

    这不是更简单

    System.IO.FileInfo f1 = new System.IO.FileInfo("c:\\myfile.txt").Length 
    

    或者你可以扩展 FileInfo 类

    public static string GetFileSize(this FileInfo fi)
    {
    
      long Bytes = fi.Length;
    
      if (Bytes >= 1073741824)
      {
         Decimal size = Decimal.Divide(Bytes, 1073741824);
         return String.Format("{0:##.##} GB", size);
      }
      else if (Bytes >= 1048576)
      {
         Decimal size = Decimal.Divide(Bytes, 1048576);
         return String.Format("{0:##.##} MB", size);
      }
      else if (Bytes >= 1024)
      {
         Decimal size = Decimal.Divide(Bytes, 1024);
         return String.Format("{0:##.##} KB", size);
      }
      else if (Bytes > 0 & Bytes < 1024)
      {
         Decimal size = Bytes;
         return String.Format("{0:##.##} Bytes", size);
      }
      else
      {
         return "0 Bytes";
      }
     }
    

    并像使用它

     System.IO.FileInfo f1 = new System.IO.FileInfo("c:\\myfile.txt");
     var size = f1.GetFileSize();
    

    【讨论】:

      【解决方案2】:

      不,但您可以创建自己的静态类并将您的方法放在那里。鉴于您基本上是在为您的用户界面生成一个摘要字符串,我认为它无论如何都不属于 File 类(即使您可以将它放在那里 - 但您不能)。

      【讨论】:

        【解决方案3】:

        Filestatic 类,不能扩展。请改用FileEx 之类的东西。

        string s = FileEx.GetFileSize("something.txt");
        

        【讨论】:

          【解决方案4】:

          不,你不能那样做。只需创建您自己的静态类并将该方法添加到其中即可。

          【讨论】:

            【解决方案5】:

            看起来你将不得不将它作为你自己的文件助手来实现。

            如果你愿意,你可以让它成为 FileInfo 的扩展方法,但是你必须做类似的事情。

            new FileInfo("某个路径").GetFileSize();

            【讨论】:

              【解决方案6】:

              您可以实现具有此类方法的新静态类,也可以扩展非静态类,例如FileStream

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2013-09-19
                • 2011-04-07
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2013-08-09
                • 2016-07-29
                相关资源
                最近更新 更多