統(tǒng)計(jì)文件字節(jié)存儲(chǔ)長(zhǎng)度(即文件大?。?,可用于存儲(chǔ)空間管理,特別是目前云存儲(chǔ)計(jì)費(fèi)按實(shí)際存儲(chǔ)字節(jié)數(shù)收費(fèi),如何將字節(jié)轉(zhuǎn)換為B,KB,GB,TB方式表示,方法如下:
(1)獲取文件長(zhǎng)度:
public int GetFileLength(string filePath){ FileInfo fi = new FileInfo(filePath);
return (int)fi.Length;}
(2)轉(zhuǎn)換為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 編輯過(guò)