是否可以扩展文件类?我想补充新GetFileSize方法File类和使用它像这样扩展文件类
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':静态类型不能用作参数“
请说明您downvote。 – 2012-11-13 20:07:52