@@ -24,14 +24,15 @@ public class DirectoryData
2424
2525 private const double BytesPerMegabyte = 1048576.0 ;
2626
27- private readonly DirectoryInfo directoryInfo ;
27+ private readonly DirectoryInfo ? directoryInfo ;
28+ private readonly object resourceLock = new ( ) ;
2829 private long size ;
2930 private long fileCount ;
3031 private long folderCount ;
31- private string name ;
32- private string fullName ;
32+ private string ? name ;
33+ private string ? fullName ;
3334 private DirectoryData [ ] subData ;
34- private Node treeMapNode = new Node ( string . Empty , 0 , 0 ) ;
35+ private Node treeMapNode = new ( string . Empty , 0 , 0 ) ;
3536
3637 #endregion
3738
@@ -65,13 +66,14 @@ public DirectoryData(string error)
6566 this . PullNodes ( ) ;
6667 }
6768
68- private DirectoryData ( DirectoryInfo directoryInfo , BackgroundWorker worker , bool reportProgress )
69+ private DirectoryData ( DirectoryInfo directoryInfo , BackgroundWorker ? worker , bool reportProgress )
6970 {
7071 this . DataType = DirectoryDataType . Directory ;
7172 this . treeMapNode . Tag = this ;
7273
7374 this . directoryInfo = directoryInfo ;
7475 this . SetName ( this . directoryInfo . Name , this . directoryInfo . FullName ) ;
76+ this . subData = CollectionUtility . EmptyArray < DirectoryData > ( ) ;
7577 this . Refresh ( worker , reportProgress ) ;
7678 }
7779
@@ -87,9 +89,9 @@ private DirectoryData(DirectoryInfo directoryInfo, BackgroundWorker worker, bool
8789
8890 public long FolderCount => this . folderCount ;
8991
90- public string Name => this . name ;
92+ public string Name => this . name ! ;
9193
92- public string FullName => this . fullName ;
94+ public string FullName => this . fullName ! ;
9395
9496 public DirectoryDataType DataType { get ; private set ; } = DirectoryDataType . Directory ;
9597
@@ -130,7 +132,7 @@ private static void AddErrorData(List<DirectoryData> subDataList, string prefix,
130132 subDataList . Add ( new DirectoryData ( prefix + message ) ) ;
131133 }
132134
133- private static bool CheckCancelled ( BackgroundWorker worker )
135+ private static bool CheckCanceled ( BackgroundWorker ? worker )
134136 {
135137 bool result = false ;
136138
@@ -142,7 +144,7 @@ private static bool CheckCancelled(BackgroundWorker worker)
142144 return result ;
143145 }
144146
145- private void Refresh ( BackgroundWorker worker , bool reportProgress )
147+ private void Refresh ( BackgroundWorker ? worker , bool reportProgress )
146148 {
147149 if ( this . directoryInfo != null )
148150 {
@@ -156,10 +158,10 @@ private void Refresh(BackgroundWorker worker, bool reportProgress)
156158 this . fileCount = 0 ;
157159 this . folderCount = 0 ;
158160
159- List < DirectoryData > subDataList = new List < DirectoryData > ( ) ;
161+ List < DirectoryData > subDataList = new ( ) ;
160162
161163 this . CalculateFileSizes ( subDataList ) ;
162- if ( ! CheckCancelled ( worker ) )
164+ if ( ! CheckCanceled ( worker ) )
163165 {
164166 if ( reportProgress )
165167 {
@@ -170,7 +172,7 @@ private void Refresh(BackgroundWorker worker, bool reportProgress)
170172 this . CalculateDirectorySizesParallel ( subDataList , worker ) ;
171173 }
172174
173- if ( ! CheckCancelled ( worker ) )
175+ if ( ! CheckCanceled ( worker ) )
174176 {
175177 this . subData = new DirectoryData [ subDataList . Count ] ;
176178 subDataList . CopyTo ( this . subData ) ;
@@ -238,7 +240,7 @@ private void CalculateFileSizes(List<DirectoryData> subDataList)
238240 int fileCount = 0 ;
239241 long fileSize = 0 ;
240242 Parallel . ForEach (
241- this . directoryInfo . EnumerateFiles ( ) ,
243+ this . directoryInfo ! . EnumerateFiles ( ) ,
242244 info =>
243245 {
244246 Interlocked . Add ( ref fileSize , info . Length ) ;
@@ -252,7 +254,7 @@ private void CalculateFileSizes(List<DirectoryData> subDataList)
252254 {
253255 // Add a faux node for files
254256 const string FilesNodeName = "[Files]" ;
255- DirectoryData data = new DirectoryData ( FilesNodeName , Path . Combine ( this . directoryInfo . FullName , FilesNodeName ) , fileSize , fileCount ) ;
257+ DirectoryData data = new ( FilesNodeName , Path . Combine ( this . directoryInfo . FullName , FilesNodeName ) , fileSize , fileCount ) ;
256258 subDataList . Add ( data ) ;
257259 }
258260 }
@@ -266,11 +268,11 @@ private void CalculateFileSizes(List<DirectoryData> subDataList)
266268 "Microsoft.Design" ,
267269 "CA1031:DoNotCatchGeneralExceptionTypes" ,
268270 Justification = "I don't want an error in one node to stop the whole process." ) ]
269- private void CalculateDirectorySizesWithProgress ( List < DirectoryData > subDataList , BackgroundWorker worker )
271+ private void CalculateDirectorySizesWithProgress ( List < DirectoryData > subDataList , BackgroundWorker ? worker )
270272 {
271273 try
272274 {
273- DirectoryInfo [ ] directories = this . directoryInfo . GetDirectories ( ) ;
275+ DirectoryInfo [ ] directories = this . directoryInfo ! . GetDirectories ( ) ;
274276
275277 // Since we're reporting the directory names as our progress, let's do them in alphabetical order.
276278 Array . Sort ( directories , ( x , y ) => string . Compare ( x . Name , y . Name , true ) ) ;
@@ -280,7 +282,7 @@ private void CalculateDirectorySizesWithProgress(List<DirectoryData> subDataList
280282 {
281283 DirectoryInfo info = directories [ i ] ;
282284
283- if ( CheckCancelled ( worker ) )
285+ if ( CheckCanceled ( worker ) )
284286 {
285287 worker = null ;
286288 break ;
@@ -311,15 +313,15 @@ private void CalculateDirectorySizesWithProgress(List<DirectoryData> subDataList
311313 "Microsoft.Design" ,
312314 "CA1031:DoNotCatchGeneralExceptionTypes" ,
313315 Justification = "I don't want an error in one node to stop the whole process." ) ]
314- private void CalculateDirectorySizesParallel ( List < DirectoryData > subDataList , BackgroundWorker worker )
316+ private void CalculateDirectorySizesParallel ( List < DirectoryData > subDataList , BackgroundWorker ? worker )
315317 {
316318 try
317319 {
318320 Parallel . ForEach (
319- this . directoryInfo . EnumerateDirectories ( ) ,
321+ this . directoryInfo ! . EnumerateDirectories ( ) ,
320322 info =>
321323 {
322- if ( ! CheckCancelled ( worker ) )
324+ if ( ! CheckCanceled ( worker ) )
323325 {
324326 this . CalculateDirectorySize ( subDataList , worker , info ) ;
325327 }
@@ -331,11 +333,11 @@ private void CalculateDirectorySizesParallel(List<DirectoryData> subDataList, Ba
331333 }
332334 }
333335
334- private void CalculateDirectorySize ( List < DirectoryData > subDataList , BackgroundWorker worker , DirectoryInfo info )
336+ private void CalculateDirectorySize ( List < DirectoryData > subDataList , BackgroundWorker ? worker , DirectoryInfo info )
335337 {
336338 // Never report progress for sub-directories. It adds too much blocking, which makes things take forever.
337339 // Add 1 to the folder count to account for the current directory.
338- DirectoryData data = new DirectoryData ( info , worker , false ) ;
340+ DirectoryData data = new ( info , worker , false ) ;
339341 this . AdjustStats ( data . Size , data . FileCount , 1 + data . FolderCount , false ) ;
340342 lock ( subDataList )
341343 {
@@ -407,9 +409,7 @@ private void AdjustStats(long sizeAdjustment, long fileCountAdjustment, long fol
407409 // If multiple threads are calculating sub-directory sizes, then the parent directory size
408410 // can be adjusted simultaneously. We need to lock on some member to ensure the size
409411 // is safely adjusted along with all dependent info (e.g., tree map size).
410- #pragma warning disable CA2002 // Do not lock on objects with weak identity. There is only one AppDomain in use in this process.
411- lock ( this . directoryInfo )
412- #pragma warning restore CA2002 // Do not lock on objects with weak identity
412+ lock ( this . resourceLock )
413413 {
414414 this . SetSize ( this . size + sizeAdjustment ) ;
415415 }
0 commit comments