3
3
4
4
using System . Collections . Concurrent ;
5
5
using System . IO ;
6
+ using System . Runtime . InteropServices ;
6
7
using Windows . Win32 ;
7
8
using Windows . Win32 . Storage . FileSystem ;
8
9
@@ -29,12 +30,12 @@ await Parallel.ForEachAsync(
29
30
_paths ,
30
31
cancellationToken ,
31
32
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 ) ) ;
38
39
39
40
unsafe void ComputeSizeRecursively ( string path , CancellationToken token )
40
41
{
@@ -73,24 +74,30 @@ unsafe void ComputeSizeRecursively(string path, CancellationToken token)
73
74
74
75
var itemPath = Path . Combine ( directory , findData . cFileName . ToString ( ) ) ;
75
76
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 ) )
77
81
{
78
- ComputeFileSize ( itemPath ) ;
82
+ continue ;
79
83
}
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 ) )
83
86
{
84
87
queue . Enqueue ( itemPath ) ;
85
88
}
89
+ else
90
+ {
91
+ ComputeFileSize ( itemPath ) ;
92
+ }
86
93
87
94
if ( token . IsCancellationRequested )
88
95
break ;
89
96
}
90
97
while ( PInvoke . FindNextFile ( hFile , & findData ) ) ;
91
- }
92
98
93
- PInvoke . CloseHandle ( hFile ) ;
99
+ PInvoke . FindClose ( hFile ) ;
100
+ }
94
101
}
95
102
}
96
103
}
0 commit comments