@@ -13,6 +13,18 @@ namespace DiskSweeper
13
13
public class SweepEngine
14
14
{
15
15
private readonly DirectoryInfo Directory ;
16
+ private const int ProgressReportInterval = 200 ;
17
+
18
+ private long TotalSize ;
19
+ private long TotalSizeOnDisk ;
20
+ private long TotalFilesCount ;
21
+ private long TotalFoldersCount ;
22
+
23
+ private long ChangesCount ;
24
+
25
+ public event EventHandler ReportProgress ;
26
+
27
+ public ( long , long , long , long ) Result => ( this . TotalSize , this . TotalSizeOnDisk , this . TotalFilesCount , this . TotalFoldersCount ) ;
16
28
17
29
public static long P0SizeFloor => ConfigurationHelper . GetConfigurationInt64 (
18
30
settingName : "DiskSweeper.Highlights.P0.SizeFloor" ,
@@ -27,6 +39,11 @@ public SweepEngine(string path)
27
39
this . Directory = new DirectoryInfo ( path ) ;
28
40
}
29
41
42
+ public SweepEngine ( DirectoryInfo dir )
43
+ {
44
+ this . Directory = dir ;
45
+ }
46
+
30
47
public ObservableCollection < DiskItem > GetDiskItems ( )
31
48
{
32
49
return new ObservableCollection < DiskItem > (
@@ -35,33 +52,27 @@ public ObservableCollection<DiskItem> GetDiskItems()
35
52
. Select ( info => new DiskItem ( info ) ) ) ;
36
53
}
37
54
38
- public static async Task < ( long , long , long , long ) > CalculateDirectorySizeRecursivelyAsync ( DirectoryInfo directory , CancellationToken cancellationToken )
55
+ public async Task CalculateDirectorySizeRecursivelyWithUpdateAsync ( DirectoryInfo directory , CancellationToken cancellationToken )
39
56
{
40
- var totalSize = 0L ;
41
- var totalSizeOnDisk = 0L ;
42
- var totalFilesCount = 0L ;
43
- var totalFoldersCount = 0L ;
44
-
45
57
try
46
58
{
47
59
foreach ( var childDirectory in directory . GetDirectories ( ) )
48
60
{
49
- var ( size , sizeOnDisk , filesCount , foldersCount ) = await SweepEngine . CalculateDirectorySizeRecursivelyAsync ( childDirectory , cancellationToken ) ;
50
- totalSize += size ;
51
- totalSizeOnDisk += sizeOnDisk ;
52
- totalFilesCount += filesCount ;
53
- totalFoldersCount += foldersCount ;
61
+ await this . CalculateDirectorySizeRecursivelyWithUpdateAsync ( childDirectory , cancellationToken ) ;
54
62
55
63
if ( cancellationToken . IsCancellationRequested )
56
64
{
57
65
break ;
58
66
}
59
67
}
60
68
61
- totalSize += directory . GetFiles ( ) . Sum ( file => file . Length ) ;
62
- //totalSizeOnDisk += directory.GetFiles().Sum(file => file.GetSizeOnDisk());
63
- totalFilesCount += directory . GetFiles ( ) . Count ( ) ;
64
- totalFoldersCount += 1 ;
69
+ var files = directory . GetFiles ( ) ;
70
+ this . TotalSize += files . Sum ( file => file . Length ) ;
71
+ //this.TotalSizeOnDisk += files.Sum(file => file.GetSizeOnDisk());
72
+ this . TotalFilesCount += files . Count ( ) ;
73
+ this . TotalFoldersCount += 1 ;
74
+
75
+ this . CountChanges ( ) ;
65
76
}
66
77
catch ( UnauthorizedAccessException ex )
67
78
{
@@ -71,8 +82,15 @@ public ObservableCollection<DiskItem> GetDiskItems()
71
82
{
72
83
Trace . WriteLine ( ex . Message ) ;
73
84
}
85
+ }
74
86
75
- return ( totalSize , totalSizeOnDisk , totalFilesCount , totalFoldersCount ) ;
87
+ private void CountChanges ( )
88
+ {
89
+ this . ChangesCount ++ ;
90
+ if ( this . ChangesCount % SweepEngine . ProgressReportInterval == 0 )
91
+ {
92
+ this . ReportProgress ? . Invoke ( this , null ) ;
93
+ }
76
94
}
77
95
}
78
96
}
0 commit comments