程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> csharp: Gets a files formatted size.,csharpformatted

csharp: Gets a files formatted size.,csharpformatted

編輯:C#入門知識

csharp: Gets a files formatted size.,csharpformatted


  /*
            ASP.NET 默認上傳文件是4M ,可以修改服務配置.. 

           <system.web>
           <!-- 指示 ASP.NET 支持的最大文件上載大小。
           該限制可用於防止因用戶將大量文件傳遞到該服務器而導致的拒絕服務攻擊。
           指定的大小以 KB 為單位。默認值為 4096 KB (4 MB)。最大為:2097151 KB
           此處改為40M大小的文件上傳限制。
           -->
           <httpRuntime maxRequestLength = "2097151" useFullyQualifiedRedirectUrl="true"/>
           </system.web>
            */

        /// <summary>
        /// Gets a files formatted size.
        /// </summary>
        /// <param name="file">The file to return size of.</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.
        /// 獲得文件大小
        /// 塗聚文
        /// 
        /// 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
        }

  

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved