統計文件字節存儲長度(即文件大?。捎糜诖鎯臻g管理,特別是目前云存儲計費按實際存儲字節數收費,如何將字節轉換為B,KB,GB,TB方式表示,方法如下:
(1)獲取文件長度:
public int GetFileLength(string filePath)
{
FileInfo fi = new FileInfo(filePath);
return (int)fi.Length;
}
(2)轉換為B,KB,GB,TB方式表示
public string GetFileSize(double FileLength)
{
string Result = "";
try
{
if (FileLength >= 1024 * 1024 * 1024)
{
if (FileLength / 1024 / 1024 / 1024 >= 1024)
Result = string.Format("{0:########0.##} T", (double)FileLength / 1024 / 1024 / 1024);
else Result = string.Format("{0:####0.##} G", (double)FileLength / 1024 / 1024 / 1024);
}
else if (FileLength >= 1024 * 1024) Result = string.Format("{0:####0.##} M", (double)FileLength / 1024 / 1024);
else if (FileLength >= 1024) Result = string.Format("{0:####0.##} K", (double)FileLength / 1024);
else if (FileLength > 0) Result = string.Format("{0:####0.##} K", FileLength / 1024);
else Result = "0 M";
}
catch (Exception ex)
{
return ex.Message;
}
return Result;
}
閱讀原文:原文鏈接
該文章在 2025/7/15 10:45:15 編輯過