In project development, it is sometimes necessary to control the size of the file, such as the need to specify the size of the file as bytes, TB, etc. Now provides a method that implements the size of the specified file,
Provides: "bytes", "KB", "MB", "GB", "TB" and other dimensions of the control options, the method code can be used directly 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 size display conversion cap /// </summary> readonly static double sizelevel = 0x400 * 0.9; /// <summary> /// Convert to size display /// </summary> / <param name= "Size > Sizes </param> /// <returns> dimensions display mode </ returns> public static string Tosizedescription (this double size) { return tosizedescription (size, 2); } /// < summary> /// conversion to Dimension display /// </summary> /// <param name= "Size" > Size </param> /// <param Name= "digits" > Decimal digits </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 size display /// </summary> /// <param name= " Size > Sizes </param> /// <returns> dimensions display mode </ returns> public static string Tosizedescription (this ulong size) { return (double) size. ToSizedescription (); } /// <summary> /// Convert to size display /// </summary> / <param name= "Size" > Size </param> /// <param name= "digits" > Number of decimal digits </param> /// <returns> size display Mode </returns> public static string tosizedescription (this ulong size, int digits) { return ( (double) size). Tosizedescription (digits); } /// <summary> /// conversion to Dimension display /// </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 size display /// </summary> /// <param name= "Size"> Size </param> /// <param name=" digits "> Number of decimal digits </param> /// <returns> size display mode </returns > public static string tosizedescription (This long size, int digits) { return (double) size. Tosizedescription (digits); } /// <summary> /// Convert to 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 size display /// </summary> /// <param name= "Size" > Size </param> / <param name= "digits" > Number of decimal digits </param> /// <returns> size display Mode </returns> public static string tosizedescription (this int size, int d igits) { return (double) size. Tosizedescription (digits); } }
The above is just a file size to perform an arithmetic operation that implements the functions described above.
This article is from the "Pengze 0902" blog, be sure to keep this source http://pengze0902.blog.51cto.com/7693836/1866717
Dotnet Specify the size of the file display