File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ using Common . BasicHelper . Utils . Extensions ;
2+ using System . Collections . Generic ;
3+ using System . IO ;
4+ using System . Linq ;
5+
6+ namespace Common . BasicHelper . IO ;
7+
8+ public class DirectoryHelper
9+ {
10+ /// <summary>
11+ /// 获取一个文件夹包括子文件夹及其子文件的总大小
12+ /// </summary>
13+ /// <param name="path">文件夹路径</param>
14+ /// <returns>总大小</returns>
15+ public static long GetDirectorySize ( string path )
16+ {
17+ var result = 0L ;
18+
19+ var dir = path . GetFullPath ( ) ;
20+
21+ if ( ! Directory . Exists ( dir ) ) return result ;
22+
23+ var pendingFolders = new Queue < DirectoryInfo > ( ) ;
24+
25+ pendingFolders . Enqueue ( new DirectoryInfo ( dir ) ) ;
26+
27+ while ( pendingFolders . Count > 0 )
28+ {
29+ var folder = pendingFolders . Dequeue ( ) ;
30+
31+ result += folder . GetFiles ( ) . Sum ( file => file . Length ) ;
32+
33+ foreach ( var subFolder in folder . GetDirectories ( ) )
34+ pendingFolders . Enqueue ( subFolder ) ;
35+ }
36+
37+ return result ;
38+ }
39+ }
You can’t perform that action at this time.
0 commit comments