zoukankan      html  css  js  c++  java
  • C#文件的大小

    前言:

    单位 描述
    bit 位.
    一个二进制数据0或1,是1bit
    byte 字节:
    存储空间的基础单位.
    1byte=8bit

     

    b=bit 表示“位”
    B=Byte 表示“字节”

    代码:

    public class FileSize
    {
        DirectoryInfo Dic;
        public FileSize(string FolderPath)
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
    
            Dic = new DirectoryInfo(FolderPath);
        }
    
        public void ListFileSize()
        {
            FileInfo[] files = Dic.GetFiles();
            if (files.Length > 0)
            {
                HttpResponse response = HttpContext.Current.Response;
                response.Write("<table style='border:solid 1px black;border-collapse:collapse;'>");
                foreach (FileInfo fi in files)
                {
                    response.Write("<tr>");
                    response.Write("<td style='border:solid 1px black'>" + fi.Name+"</td>");
                    response.Write("<td style='border:solid 1px black'>" + CalculateSize(fi.Length) + "</td>");
                    response.Write("</tr>");
                }
    
                response.Write("</table>");
            }
        }
    
        private string CalculateSize(long size)
        {
            string length = string.Empty;
    
            if (size < 1024)
            {
                length = size + "bytes";
            }
            else
                if (size < 1024 * 1024)
                {
                    length = float.Parse((size * 10 / 1024).ToString()) / 10 + "KB";
                }
                else
                    if (size < 1024 * 1024 * 1024)
                    {
                        length = float.Parse((size * 10 / 1048576).ToString()) / 10 + "MB";
                    }
                    else
                    {
                        length = float.Parse((size * 10 / 1073741824).ToString()) / 10 + "GB";
                    }
    
            return length;
        }
        
    }

    效果:

    image

  • 相关阅读:
    民宿项目知识_服务器路径与文件的存储
    民宿项目_mysql_jdbc
    Apple Mach-O Linker Warning
    ios控制器视图载入周期小记
    StatusBar style的那点事
    oc--单例设计模式
    gcd笔记
    【转载】10年的程序员生涯(附带原文地址)
    NSProxy使用笔记
    UINavigationController的视图层理关系
  • 原文地址:https://www.cnblogs.com/oneword/p/1569129.html
Copyright © 2011-2022 走看看