文件大小从字节byte化为可读单位

date
Nov 28, 2021
slug
10033
status
Published
tags
C#
summary
type
Post
文件大小从字节byte化为可读单位
string[] suf = { "B", "KB", "MB", "GB", "TB", "PB", "EB" }; //Longs run out around EB
long fileLengthInByte = new FileInfo(fname).Length;
int place = Convert.ToInt32(Math.Floor(Math.Log(fileLengthInByte, 1024)));
double fileLengthReadable = Math.Round(fileLengthInByte / Math.Pow(1024, place), 3);
使用:
string.Format("File size readable:\\t{0:0.000} {1}{2}", fileLengthReadable, suf[place], Environment.NewLine);
 

© Wen Bo 2021 - 2022