2个方法得到并返回文件大小,

第一个方法得到文件大小,并自动返回与文件详细信息里面的显示一致

第二个方法得到文件大小,并自动按字节大小返回指定小数位数的大小信息

       /// <summary>
        /// 方法一,得到文件大小,返回文件大小(与文件详细信息里的大小一致)
        /// </summary>
        /// <param name="filename">带路径的文件名</param>
        /// <returns></returns>
        private static string GetFi0leSize(string filename)
        {
            string result = "";
            System.IO.FileInfo fileInfo = new System.IO.FileInfo(filename);
            double filesize = fileInfo.Length;
            long gb = 1024 * 1024 * 1024;
            long mb = 1024 * 1024;
            long kb = 1024;
            double temp = 0;
            if (filesize <= 0)
            {
                return "0";
            }
            else if (filesize >= 1024000000) 
            {
                temp = filesize / gb;
                if (temp >= 100)
                {
                    result = ((int)temp).ToString();
                }
                else if (temp >= 10)
                {
                    result = ((int)(temp * 10) / 10.0).ToString("0.0");
                }
                else
                {
                    result = ((int)(temp * 100) / 100.0).ToString("0.00");
                }
                return result + "GB";
            }
            else if (filesize >= 1024000) 
            {
                temp = filesize / mb;
                if (temp >= 100)
                {
                    result = ((int)temp).ToString();
                }
                else if (temp >= 10)
                {
                    result = ((int)(temp * 10) / 10.0).ToString("0.0");
                }
                else
                {
                    result = ((int)(temp * 100) / 100.0).ToString("0.00");
                }
                return result + "MB";
            }
            else if (filesize >= 1024) 
            {
                temp = filesize / kb;
                if (temp >= 100)
                {
                    result = ((int)temp).ToString();
                }
                else if (temp >= 10)
                {
                    result = ((int)(temp * 10) / 10.0).ToString("0.0");
                }
                else
                {
                    result = ((int)(temp * 100) / 100.0).ToString("0.00");
                }
                return result + "KB";
            }
            else
            {
                return filesize.ToString() + "B";
                //return string.Format("{0:0.00} bytes", filesize);
            }
        }
        /// <summary>
        /// 方法二,得到文件大小并格式化返回
        /// </summary>
        /// <param name="filename">带路径的文件名</param>
        /// <param name="weishu">保留小数位数</param>
        /// <returns></returns>
        private static string GetFileSize(string filename, int weishu)
        {
            System.IO.FileInfo fileInfo = new System.IO.FileInfo(filename);
            double filesize = fileInfo.Length;
            long gb = 1024 * 1024 * 1024;
            long mb = 1024 * 1024;
            long kb = 1024;
            if (filesize <= 0)
            {
                return "0";
            }
            else if (filesize >= gb) 
            {
                return Math.Round((double)filesize / gb, weishu, MidpointRounding.AwayFromZero).ToString() + "GB";
                //return string.Format("{0:0.00} GB", (double)filesize / (1024 * 1024 * 1024));
            }
            else if (filesize >= mb) 
            {
                return Math.Round((double)filesize / mb, weishu, MidpointRounding.AwayFromZero).ToString() + "MB";
                //return string.Format("{0:0.00} MB", (double)filesize / (1024 * 1024));
            }
            else if (filesize >= kb) 
            {
                return Math.Round((double)filesize / kb, weishu, MidpointRounding.AwayFromZero).ToString() + "KB";
                //return string.Format("{0:0.00} KB", (double)filesize / 1024);
            }
            else
            {
                return filesize.ToString() + "B";
                //return string.Format("{0:0.00} bytes", filesize);
            }
        }