是否可以擴展文件類?我想補充新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