48 lines
1.3 KiB
C#
48 lines
1.3 KiB
C#
![]() |
namespace AlicizaX.Runtime
|
|||
|
{
|
|||
|
public static partial class Utility
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// 文件相关的实用函数
|
|||
|
/// </summary>
|
|||
|
public static class File
|
|||
|
{
|
|||
|
private static readonly string[] UnitList = new[] {"B", "KB", "MB", "GB", "TB", "PB"};
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 获取字节大小
|
|||
|
/// </summary>
|
|||
|
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";
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
}
|