[转载]获取文件夹的大小 递归

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;
}
posted @ 2008-05-06 11:49  RobotTech  阅读(255)  评论(0编辑  收藏  举报