Csharp: Gets a files formatted size., csharpformatted

Source: Internet
Author: User

Csharp: Gets a files formatted size., csharpformatted

/* ASP. NET uploads a file of 4 MB by default. You can modify the service configuration... <system. web> <! -- Indicates the maximum file upload size supported by ASP. NET. This restriction can be used to prevent DoS attacks caused by a large number of files being transferred to the server. The specified size is in KB. The default value is 4096 KB (4 MB ). Maximum Size: 2097151 KB. The size is changed to 40 MB. --> <HttpRuntime maxRequestLength = "2097151" useFullyQualifiedRedirectUrl = "true"/> </system. web> * //// <summary> // Gets a files formatted size. /// </summary> /// <param name = "file"> The file to return size. </param> /// <returns> </returns> // public static string GetFileSize (this FileInfo file) // {// try // {// determine all file sizes // double sizeinbytes = file. length; // double sizeinkbytes = Math. round (sizeinbytes/1024); // double sizeinmbytes = Math. round (sizeinkbytes/1024); // double sizeingbytes = Math. round (sizeinmbytes/1024); // if (sizeingbytes> 1) // return string. format ("{0} GB", sizeingbytes); // returns size in gigabytes // else if (sizeinmbytes> 1) // return string. format ("{0} MB", sizeinmbytes); // returns size in megabytes if less than one gigabyte // else if (sizeinkbytes> 1) // return string. format ("{0} KB", sizeinkbytes); // returns size in kilabytes if less than one megabyte // else // return string. format ("{0} B", sizeinbytes); // returns size in bytes if less than one kilabyte //} // catch {return "Error Getting Size ";} // catches any possible error and just returns error getting size //} // <summary> // Gets a files formatted size. /// get the file size /// tu juwen //// Geovin Du /// </summary> /// <param name = "file"> </param >/// <returns> </returns> public string GetFileSize (double file) {try {// 1. double byteSize = Math. round (file/1024*100) * 0.01; string suffix = "KB"; if (byteSize> 1000) {byteSize = Math. round (byteSize *. 001*100 )*. 01; suffix = "MB";} double byteGSize = Math. round (byteSize/1024*100) * 0.01; if (byteGSize> 1000) {byteGSize = Math. round (byteGSize *. 001*100 )*. 01; suffix = "GB";} return byteSize. toString () + suffix; // 2. // determine all file sizes // double sizeinbytes = file; // double sizeinkbytes = Math. round (sizeinbytes/1024); // double sizeinmbytes = Math. round (sizeinkbytes/1024); // double sizeingbytes = Math. round (sizeinmbytes/1024); // if (sizeingbytes> 1) // return string. format ("{0: 0. 00} GB ", sizeingbytes); // returns size in gigabytes // else if (sizeinmbytes> 1) // return string. format ("{0: 0. 00} MB ", sizeinmbytes); // returns size in megabytes if less than one gigabyte // else if (sizeinkbytes> 1) // return string. format ("{0: 0. 00} KB ", sizeinkbytes); // returns size in kilabytes if less than one megabyte // else // return string. format ("{0} B", sizeinbytes); // returns size in bytes if less than one kilabyte} catch {return "Error Getting Size ";} // catches any possible error and just returns error getting size}

  

Related Article

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.