格式化一个文件的大小,虽然很简单,但却是很常用的,这里分享一个C#写的格式化文件大小的方法:
public static String FormatFileSize(Int64 fileSize)
{
if (fileSize < 0)
{
throw new ArgumentOutOfRangeException('fileSize');
}
else if (fileSize >= 1024 * 1024 * 1024)
{
return string.Format('{0:########0.00} GB', ((Double)fileSize) / (1024 * 1024 * 1024));
}
else if (fileSize >= 1024 * 1024)
{
return string.Format('{0:####0.00} MB', ((Double)fileSize) / (1024 * 1024));
}
else if (fileSize >= 1024)
{
return string.Format('{0:####0.00} KB', ((Double)fileSize) / 1024);
}
else
{
return string.Format('{0} bytes', fileSize);
}
}
本文介绍了一个实用的C#方法,用于将文件大小转换为易于理解的格式,如GB、MB、KB或bytes,适用于各种文件管理和显示场景。
220

被折叠的 条评论
为什么被折叠?



