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

转载于:https://www.cnblogs.com/oneword/archive/2009/09/18/1569129.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值