1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using System . IO ;
4
+ using System . Linq ;
5
+ using System . Runtime . CompilerServices ;
6
+ using System . Runtime . InteropServices ;
7
+ using System . Threading ;
8
+ using System . Threading . Tasks ;
9
+
10
+ namespace Unget
11
+ {
12
+ class Program
13
+ {
14
+ static void Main ( string [ ] args )
15
+ {
16
+ try
17
+ {
18
+ Console . WriteLine ( "unget - NuGet Cache Cleaner\n " ) ;
19
+
20
+ string [ ] dirs
21
+ = Directory . GetDirectories ( NuGetPaths . GetNugetCachePathByPlatform ( ) ,
22
+ "*.*" , SearchOption . TopDirectoryOnly ) ;
23
+
24
+ int revomalDirCount = 0 ;
25
+
26
+ Parallel . For ( 0 , dirs . Length , i =>
27
+ {
28
+ List < string > cachedDirs
29
+ = Directory . GetDirectories ( dirs [ i ] , "*.*" , SearchOption . TopDirectoryOnly ) . ToList ( ) ;
30
+
31
+ if ( cachedDirs . Count > 1 )
32
+ {
33
+ Interlocked . Add ( ref revomalDirCount , cachedDirs . Count - 1 ) ;
34
+ cachedDirs . Sort ( ) ;
35
+
36
+ for ( int j = 0 ; j < cachedDirs . Count - 1 ; j ++ )
37
+ {
38
+ try
39
+ {
40
+ Directory . Delete ( cachedDirs [ j ] , true ) ;
41
+ Console . WriteLine ( $ "[DEL] { cachedDirs [ j ] } ") ;
42
+ }
43
+ catch ( Exception exception )
44
+ {
45
+ Console . WriteLine ( $ "\n [ERR] { cachedDirs [ j ] } \n { exception } \n ") ;
46
+ }
47
+ }
48
+ }
49
+ } ) ;
50
+
51
+ Console . WriteLine ( $ "\n Total { revomalDirCount } folder(s) are deleted from cache.") ;
52
+
53
+ Console . ReadKey ( false ) ;
54
+ }
55
+ catch ( Exception exception )
56
+ {
57
+ Console . WriteLine ( exception ) ;
58
+ }
59
+ }
60
+ }
61
+ }
0 commit comments