zoukankan      html  css  js  c++  java
  • 获取指定路径下文件夹所有文件的大小

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;

    using System.IO;

    namespace getHtmlByTime
    {
     /// <summary>
     /// getFolderLength 的摘要说明。
     /// </summary>
     public class getFolderLength : System.Web.UI.Page
     {
      private void Page_Load(object sender, System.EventArgs e)
      {
       //string path = Server.MapPath("my");
       string path = @"D:\wwwroot\";
       long len = GetDirectoryLength(path);
       string dirLen = Math.Round((double)len/1024/1024,2)+"MB";
       //string dirLen = string.Format("{0:n2}MB",MyDouble);
       Response.Write(dirLen);
      }

      protected string ShowSize(object size)
      {
       int length = Convert.ToInt32(size);
       if(length>=(1024*1024))
       {
        return  Math.Round((double)length/(1024*1024),2) +" MB";
       }
       else
       {
        return  Math.Round((double)length/(1024),2)+" KB";
       }
      }

      public long GetDirectoryLength(string dirPath)
      {
       if(!Directory.Exists(dirPath))
        return 0;
       long len=0;
       DirectoryInfo di=new DirectoryInfo(dirPath);
       foreach(FileInfo fi in di.GetFiles())
       {
        len+=fi.Length;
       }
       DirectoryInfo[] dis=di.GetDirectories();
       if(dis.Length>0)
       {
        for(int i=0;i<dis.Length;i++)
        {
         len+=GetDirectoryLength(dis[i].FullName);
        }
       }
       return len;
      }

      #region Web 窗体设计器生成的代码
      override protected void OnInit(EventArgs e)
      {
       //
       // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
       //
       InitializeComponent();
       base.OnInit(e);
      }
      
      /// <summary>
      /// 设计器支持所需的方法 - 不要使用代码编辑器修改
      /// 此方法的内容。
      /// </summary>
      private void InitializeComponent()
      {   
       this.Load += new System.EventHandler(this.Page_Load);

      }
      #endregion
     }
    }

  • 相关阅读:
    天梯赛5-12 愿天下有情人都是失散多年的兄妹 【dfs】
    poj2718 Smallest Difference【贪心】
    HDU problem 5635 LCP Array【思维】
    codeforces 782C Andryusha and Colored Balloons【构造】
    HDU 4278 Faulty Odometer【进制转换】
    codeforces B. The Meeting Place Cannot Be Changed【二分】
    POJ 3264 Balanced Lineup 【线段树】
    HDU 1850
    CodeForces-714C
    HDU Problem 1247 Hat's Words 【字典树】
  • 原文地址:https://www.cnblogs.com/cztom/p/775325.html
Copyright © 2011-2022 走看看