2014-09-01 150 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); 
}