c# 获取文件大小

2023-05-10 13:19 478 浏览

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);
            }
        }
c#
分享:
上一篇 下一篇
暂无资料
相关内容0
  • c#判断字符串为英文字母
    public static bool IsEn(string input) //判断字符串是不是全部为英文字母 { string pattern = @"^[A-Za-z]+$"; Regex regex = new Regex(pattern); return regex.IsMatch(input); }
  • c#判断字符串是不是全部为数字
    public static bool IsNum(string input) //判断字符串是不是全部为数字 { string pattern = @"^[0-9]+$"; Regex regex = new Regex(pattern); return regex.IsMatch(input); }
  • c#获取鼠标的位置和对应的句柄
    [DllImport("user32.dll")]static extern IntPtr WindowFromPoint(Point Point); [DllImport("user32.dll")]static extern bool GetCursorPos(out Point lpPoint);[DllImport("user32.dll")]static extern int SetCursorPos(int x, int y); Point p; if (GetCursorPos(out p)) { text=p.X + "." + p.Y; //返回鼠标位置坐标 } Text = WindowFromPoint(p).ToString (); //返回坐标处的句柄 SetCursorPos(0,0); //设置鼠标坐标
相关推荐
程序设计
热门内容
  • c#获取鼠标的位置和对应的句柄
    [DllImport("user32.dll")]static extern IntPtr WindowFromPoint(Point Point); [DllImport("user32.dll")]static extern bool GetCursorPos(out Point lpPoint);[DllImport("user32.dll")]static extern int SetCursorPos(int x, int y); Point p; if (GetCursorPos(out p)) { text=p.X + "." + p.Y; //返回鼠标位置坐标 } Text = WindowFromPoint(p).ToString (); //返回坐标处的句柄 SetCursorPos(0,0); //设置鼠标坐标
  • [源码]PLC 推箱子 游戏
    介绍 本人是工控菜鸟,初学PLC编程。 最近一直在学习 小羽老师 的教学课程。 本程序是用WinCC7.5SP2 配合博图15.1 开发。WinCC只负责游戏画面的显示部分,其他功能逻辑的处理则全部由PLC来完成。(PLC设备是仿真的西门子1200系列的1214C) 本程序只是为了熟悉梯形图程序设计,只是为了编程而写的PLC程序,大佬们不要喷我! 截图 下载地址 PLC推箱子_源码.rar 相关软件
  • 卡通手绘芦荟绿色芦荟汁液美容院海报PNG免扣素材
    卡通手绘芦荟绿色芦荟汁液美容院海报PNG免扣素材 下载地址 142卡通手绘芦荟绿色芦荟汁液美容院海报PNG免扣素材.rar
  • c#判断字符串是不是全部为数字
    public static bool IsNum(string input) //判断字符串是不是全部为数字 { string pattern = @"^[0-9]+$"; Regex regex = new Regex(pattern); return regex.IsMatch(input); }
  • C#图形处理三种方式
    本代码通过彩色图象灰度化来介绍C#处理数字图像的3种方法,Bitmap类、BitmapData类和Graphics类是C#处理图像的的3个重要的类。 Bitmap主要要用于处理由像素数据定义的图像的对象,主要方法和属性如下:     GetPixel方法和SetPixel方法,获取和设置一个图像的指定像素的颜色。     PixelFormat属性,返回图像的像素格式。     Palette属性,获取
Tags标签
联系方式