Dotnet Specify the size of the file display

Source: Internet
Author: User
Tags dotnet

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 &LT;/RETURNS&GT;        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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.