namespace AlicizaX
{
public static partial class Utility
{
///
/// 文件相关的实用函数
///
public static class File
{
private static readonly string[] UnitList = new[] {"B", "KB", "MB", "GB", "TB", "PB"};
///
/// 获取字节大小
///
public static string GetBytesSize(long size)
{
foreach (var unit in UnitList)
{
if (size <= 1024)
{
return size + unit;
}
size /= 1024;
}
return size + UnitList[0];
}
public static string GetLengthString(long length)
{
if (length < 1024)
{
return $"{length.ToString()} Bytes";
}
if (length < 1024 * 1024)
{
return $"{(length / 1024f):F2} KB";
}
return length < 1024 * 1024 * 1024 ? $"{(length / 1024f / 1024f):F2} MB" : $"{(length / 1024f / 1024f / 1024f):F2} GB";
}
}
}
}