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}