扩展文件类
是否可以扩展文件类?我想向文件类添加新的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不,但您可以创建自己的静态类并将方法放在那里。鉴于您基本上是为用户界面生成一个摘要字符串,我不认为它无论如何都属于 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).
这不是更简单吗
?或者您可以扩展 FileInfo 类
并像这样使用它
Wouldn't this simpler
or you can extend the FileInfo class
And use it like
File
是一个static
类,无法扩展。请改用FileEx
之类的东西。File
is astatic
class and cannot be extended. Use something likeFileEx
instead.不,你不能那样做。只需创建您自己的静态类并将该方法添加到其中即可。
No, you can't do that. Just create your own static class and add that method to it.
看起来你必须将其实现为你自己的文件助手。
如果您愿意,您可以将其设为 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();
您可以实现一个具有此类方法的新静态类,也可以扩展一个非静态类,例如
FileStream
。You can either implement a new static class that would have such a method or extent a non-static class like
FileStream
.