33
44using System . Collections . Concurrent ;
55using System . IO ;
6+ using System . Runtime . InteropServices ;
67using Windows . Win32 ;
78using Windows . Win32 . Storage . FileSystem ;
89
@@ -29,12 +30,12 @@ await Parallel.ForEachAsync(
2930 _paths ,
3031 cancellationToken ,
3132 async ( path , token ) => await Task . Factory . StartNew ( ( ) =>
32- {
33- ComputeSizeRecursively ( path , token ) ;
34- } ,
35- token ,
36- TaskCreationOptions . LongRunning ,
37- TaskScheduler . Default ) ) ;
33+ {
34+ ComputeSizeRecursively ( path , token ) ;
35+ } ,
36+ token ,
37+ TaskCreationOptions . LongRunning ,
38+ TaskScheduler . Default ) ) ;
3839
3940 unsafe void ComputeSizeRecursively ( string path , CancellationToken token )
4041 {
@@ -73,24 +74,30 @@ unsafe void ComputeSizeRecursively(string path, CancellationToken token)
7374
7475 var itemPath = Path . Combine ( directory , findData . cFileName . ToString ( ) ) ;
7576
76- if ( attributes . HasFlag ( FILE_FLAGS_AND_ATTRIBUTES . FILE_ATTRIBUTE_DIRECTORY ) )
77+ // Skip current and parent directory entries
78+ var fileName = findData . cFileName . ToString ( ) ;
79+ if ( fileName . Equals ( "." , StringComparison . OrdinalIgnoreCase ) ||
80+ fileName . Equals ( ".." , StringComparison . OrdinalIgnoreCase ) )
7781 {
78- ComputeFileSize ( itemPath ) ;
82+ continue ;
7983 }
80- else if ( findData . cFileName . ToString ( ) is string fileName &&
81- fileName . Equals ( "." , StringComparison . OrdinalIgnoreCase ) &&
82- fileName . Equals ( ".." , StringComparison . OrdinalIgnoreCase ) )
84+
85+ if ( attributes . HasFlag ( FILE_FLAGS_AND_ATTRIBUTES . FILE_ATTRIBUTE_DIRECTORY ) )
8386 {
8487 queue . Enqueue ( itemPath ) ;
8588 }
89+ else
90+ {
91+ ComputeFileSize ( itemPath ) ;
92+ }
8693
8794 if ( token . IsCancellationRequested )
8895 break ;
8996 }
9097 while ( PInvoke . FindNextFile ( hFile , & findData ) ) ;
91- }
9298
93- PInvoke . CloseHandle ( hFile ) ;
99+ PInvoke . FindClose ( hFile ) ;
100+ }
94101 }
95102 }
96103 }
0 commit comments