扩展文件类

发布于 2024-10-22 04:43:28 字数 997 浏览 5 评论 0原文

是否可以扩展文件类?我想向文件类添加新的 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':静态类型不能用作参数”

Is it possible to extend File Class? I would like to add new GetFileSize Method to File Class and use it like this

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

Implementation

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

I have tried to use Extension Methods to add the method to File Class but compiler give error "'System.IO.File': static types cannot be used as parameters"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

傾旎 2024-10-29 04:43:28

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

No, but you can just create your own static class and put your methods there. Given that you are basically producing a summary string for your user interface, I wouldn't think that it would belong within the File class anyway (even if you could put it there - which you can't).

吝吻 2024-10-29 04:43:28

这不是更简单吗

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

Wouldn't this simpler

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

or you can extend the FileInfo class

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

And use it like

 System.IO.FileInfo f1 = new System.IO.FileInfo("c:\\myfile.txt");
 var size = f1.GetFileSize();
谁的年少不轻狂 2024-10-29 04:43:28

File 是一个static 类,无法扩展。请改用 FileEx 之类的东西。

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

File is a static class and cannot be extended. Use something like FileEx instead.

string s = FileEx.GetFileSize("something.txt");
仅一夜美梦 2024-10-29 04:43:28

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

No, you can't do that. Just create your own static class and add that method to it.

小梨窩很甜 2024-10-29 04:43:28

看起来你必须将其实现为你自己的文件助手。

如果您愿意,您可以将其设为 FileInfo 的扩展方法,但随后您必须执行类似的操作。

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

Looks like you're gonna have to just implement that as your own file helper.

You could make it an extension method of FileInfo if you wanted but then you'd have to do something like.

new FileInfo("some path").GetFileSize();

梦太阳 2024-10-29 04:43:28

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

You can either implement a new static class that would have such a method or extent a non-static class like FileStream.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文