架构师_程序员_码农网

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

搜索
查看: 27912|回复: 2

[资料] 文件大小Bytes到KB,MB,GB,TB单位智能转换

[复制链接]
发表于 2016-8-24 15:43:07 | 显示全部楼层 |阅读模式
QQ截图20160824154017.jpg QQ截图20160824154025.jpg


  1. private void button3_Click(object sender, EventArgs e)
  2.         {
  3.             using (OpenFileDialog ofd = new OpenFileDialog())
  4.             {
  5.                 ofd.Filter = "图片|*.jpg;*.png;*.gif;*.jpeg;*.bmp";
  6.                 if (ofd.ShowDialog() == DialogResult.OK)
  7.                 {
  8.                     using (FileStream file = new FileStream(ofd.FileName, FileMode.Open))
  9.                     {
  10.                         long size = file.Length;
  11.                         MessageBox.Show(ConvertBytes(size));
  12.                     }

  13.                 }
  14.             }
  15.         }

  16.         /// <summary>
  17.         /// Bytes到KB,MB,GB,TB单位智能转换
  18.         /// </summary>
  19.         /// <param name="len"></param>
  20.         /// <returns></returns>
  21.         public static string ConvertBytes(long len)
  22.         {
  23.             string[] sizes = { "Bytes", "KB", "MB", "GB", "TB" };
  24.             int order = 0;
  25.             while (len >= 1024 && order + 1 < sizes.Length)
  26.             {
  27.                 order++;
  28.                 len = len / 1024;
  29.             }
  30.             return String.Format("{0:0.##} {1}", len, sizes[order]);
  31.         }
复制代码






上一篇:c#大图片压缩算法,减少图片体积
下一篇:c#写的lol垃圾清理器优化内存[源码]
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
发表于 2017-11-18 22:54:17 | 显示全部楼层
/// <summary>
        /// Bytes到KB,MB,GB,TB单位智能转换
        /// </summary>
        /// <param name="len"></param>
        /// <returns></returns>
        public static string ConvertBytes(long len)
        {
            //string[] sizes = { "Bytes", "KB", "MB", "GB", "TB" };
            //int order = 0;
            //while (len >= 1024 && order + 1 < sizes.Length)
            //{
            //    order++;
            //    len = len / 1024;
            //}
            //return String.Format("{0:0.##} {1}", len, sizes[order]);
            double leng = Convert.ToDouble(len);
            string[] sizes = { "Bytes", "KB", "MB", "GB", "TB" };
            int order = 0;
            while (leng >= 1024 && order + 1 < sizes.Length)
            {
                order++;
                leng = leng / 1024;
            }
            return String.Format("{0:0.##} {1}", leng, sizes[order]);
        }
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
 楼主| 发表于 2023-6-9 20:32:25 | 显示全部楼层
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

免责声明:
码农网所发布的一切软件、编程资料或者文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:help@itsvse.com

QQ|小黑屋|架构师 ( 鲁ICP备14021824号-2 )|网站地图

GMT+8, 2026-8-30 06:50

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表