2014-09-01 126 views
0

迭代文件夹和子文件夹以获得从指定位置开始的每个文件夹的总大小的最佳方式是什么?文件总大小和子文件夹

少量样品:

C:/,d:/,F:/游戏,G:/ BLA/BLA/BLA,

我想,检查根目录的他尺寸的梅索德... 所以像这样:

C:/ totalsize = 1000GB,D:/ total size = 1000GB,F:/游戏总大小F:/ = 1000GB,G:/ bla/bla/bla,total G的大小:/ = 1000GB

foreach (string directory in drives) 
     { 
      if (directory == GoPro1 || directory == GoPro2 || directory == GoPro3 || directory == GoPro4) 
      { 
       //gets drive letter from directory location 
       var drive = Path.GetPathRoot(directory); 

       //create new drive info 
       DriveInfo di = new DriveInfo(drive); 

       verbruik[teller1] = ("Total Size " + di.TotalSize/1024/1024/1024 + " GB " + "Free Space " + di.AvailableFreeSpace/1024/1024/1024 + "GB" + "  Total Sub dir: " + drive); 
       teller1 = teller1 + 1; 
      } 

     } 

Th是我有,但这不会显示所选子文件夹的总大小。

回答

1
public static long GetFolderSize(string directory) 
{ 
    return new DirectoryInfo(directory).GetFiles("*.*", SearchOption.AllDirectories).Sum(file => file.Length); 
}