获取指定路径下文件夹所有文件的大小

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
 }
}

posted @ 2007-06-07 15:10  亦续缘  阅读(426)  评论(0编辑  收藏  举报