using System; namespace CZGL.SystemInfo { /// /// 大小信息 /// public struct SizeInfo { /// /// Byte 长度 /// public long ByteLength { get; private set; } /// /// 大小 /// public decimal Size { get; set; } /// /// 单位 /// public UnitType SizeType { get; set; } /// /// 将字节单位转换为合适的单位 /// /// 字节长度 /// public static SizeInfo Get(long byteLength) { UnitType unit = 0; decimal number = byteLength; if (byteLength < 1000) { return new SizeInfo() { ByteLength = byteLength, Size = byteLength, SizeType = UnitType.B }; } // 避免出现 1023B 这种情况;这样 1023B 会显示 0.99KB while (Math.Round(number / 1000) >= 1) { number = number / 1024; unit++; } return new SizeInfo { Size = Math.Round(number, 2), SizeType = unit, ByteLength = byteLength }; throw new Exception(); } } }