DotNet specifies the file display size, dotnet specifies the size
During project development, you sometimes need to control the file size, for example, you need to specify the file size as byte, TB, and so on. Now we provide a method to specify the file size,
Provides: "Byte", "KB", "MB", "GB", "TB" and other size control options. The following code can be directly used in the project, improve project development efficiency.
[System. componentModel. editorBrowsable (System. componentModel. editorBrowsableState. never)] public static class FsLibValueExtract {readonly static string [] SizeDefinitions = {"Byte", "KB", "MB", "GB", "TB "}; /// <summary> // control the upper limit of Size Display conversion /// </summary> readonly static double SizeLevel = 0x400*0.9; /// <summary> /// convert to the Dimension Display Mode. /// </summary> /// <param name = "size"> size </param> /// <returns> size display mode </returns> public static string ToSizeDescription (this double size) {return ToSizeDescription (size, 2 );} /// <summary> /// convert to the Dimension Display Mode. /// </summary> /// <param name = "size"> size </param> /// <param name = "digits"> decimal places </param> // <returns> size display mode </returns> public static string ToSizeDescription (this double size, int digits) {var sizeDefine = 0; while (sizeDefine <SizeDefinitions. length & size> SizeLevel) {size/= 0x400; sizeDefine ++;} if (sizeDefine = 0) return size. toString ("#0") + "" + SizeDefinitions [sizeDefine]; return size. toString ("#0. "+ string. empty. padLeft (digits, '#') + "" + SizeDefinitions [sizeDefine];} /// <summary> /// convert to the Dimension Display Mode. /// </summary> /// <param name = "size"> size </param> /// <returns> size display mode </returns> public static string ToSizeDescription (this ulong size) {return (double) size ). toSizeDescription ();} /// <summary> /// convert to the Dimension Display Mode. /// </summary> /// <param name = "size"> size </param> /// <param name = "digits"> decimal places </param> // <returns> display mode </returns> public static string ToSizeDescription (this ulong size, int digits) {return (double) size ). toSizeDescription (digits );} /// <summary> /// convert to the Dimension Display Mode. /// </summary> /// <param name = "size"> size </param> /// <returns> size display mode </returns> public static string ToSizeDescription (this long size) {return (double) size ). toSizeDescription ();} /// <summary> /// convert to the Dimension Display Mode. /// </summary> /// <param name = "size"> size </param> /// <param name = "digits"> decimal places </param> // <returns> display mode </returns> public static string ToSizeDescription (this long size, int digits) {return (double) size ). toSizeDescription (digits );} /// <summary> /// convert to the Dimension Display Mode. /// </summary> /// <param name = "size"> size </param> /// <returns> size display mode </returns> public static string ToSizeDescription (this int size) {return (double) size ). toSizeDescription ();} /// <summary> /// convert to the Dimension Display Mode. /// </summary> /// <param name = "size"> size </param> /// <param name = "digits"> decimal places </param> // <returns> display mode </returns> public static string ToSizeDescription (this int size, int digits) {return (double) size ). toSizeDescription (digits );}}
The above is only an arithmetic operation on the file size to implement the functions described above.