@@ -40,24 +40,29 @@ private static void Delete(string path, string displayPath)
40
40
displayPath = "..." + displayPath . Substring ( displayPath . Length - 61 , 61 ) ;
41
41
}
42
42
43
- if ( System . IO . File . Exists ( path ) )
43
+ if ( Program . ExecuteOperation ( ( ) => System . IO . File . Exists ( path ) ) )
44
44
{
45
45
System . Console . WriteLine ( "Delete file {0}" , displayPath ) ;
46
- System . IO . File . Delete ( path ) ;
46
+
47
+ Program . DeleteFile ( path ) ;
48
+
47
49
return ;
48
50
}
49
51
50
- if ( System . IO . Directory . Exists ( path ) )
52
+ if ( Program . ExecuteOperation ( ( ) => System . IO . Directory . Exists ( path ) ) )
51
53
{
52
54
var shortPath = Program . ShortenDir ( path ) ;
53
55
54
56
System . Console . WriteLine ( "Analyzing {0}" , displayPath ) ;
55
57
56
- var entries = System . IO . Directory . GetFileSystemEntries ( shortPath ) ;
58
+ var entries = Program . ExecuteOperation ( ( ) => System . IO . Directory . GetFileSystemEntries ( shortPath ) ) ;
57
59
58
- foreach ( var entry in entries )
60
+ if ( entries != null )
59
61
{
60
- Program . Delete ( entry , System . IO . Path . Combine ( fullDisplayPath , System . IO . Path . GetFileName ( entry ) ) ) ;
62
+ foreach ( var entry in entries )
63
+ {
64
+ Program . Delete ( entry , System . IO . Path . Combine ( fullDisplayPath , System . IO . Path . GetFileName ( entry ) ) ) ;
65
+ }
61
66
}
62
67
63
68
System . Console . WriteLine ( "Delete dir {0}" , displayPath ) ;
@@ -66,18 +71,52 @@ private static void Delete(string path, string displayPath)
66
71
}
67
72
}
68
73
69
- private static void DeleteDir ( string newPath )
74
+ private static void DeleteFile ( string path )
75
+ {
76
+ Program . ExecuteOperation ( ( ) => System . IO . File . Delete ( path ) ) ;
77
+ }
78
+
79
+ private static void DeleteDir ( string path )
80
+ {
81
+ Program . ExecuteOperation ( ( ) => System . IO . Directory . Delete ( path ) ) ;
82
+ }
83
+
84
+ private static T ExecuteOperation < T > ( System . Func < T > operation )
85
+ {
86
+ T result = default ( T ) ;
87
+
88
+ Program . ExecuteOperation ( ( ) =>
89
+ {
90
+ result = operation ( ) ;
91
+ } ) ;
92
+
93
+ return result ;
94
+ }
95
+
96
+ private static void ExecuteOperation ( System . Action operation )
70
97
{
71
98
try
72
99
{
73
- System . IO . Directory . Delete ( newPath ) ;
100
+ operation ( ) ;
74
101
}
75
102
catch ( System . IO . IOException ex )
76
103
{
77
- System . Console . WriteLine ( ex . Message ) ;
104
+ Program . Display ( ex ) ;
105
+ }
106
+ catch ( System . UnauthorizedAccessException ex )
107
+ {
108
+ Program . Display ( ex ) ;
78
109
}
79
110
}
80
111
112
+ private static void Display ( System . Exception ex )
113
+ {
114
+ var color = System . Console . ForegroundColor ;
115
+
116
+ System . Console . ForegroundColor = System . ConsoleColor . Red ;
117
+ System . Console . Error . WriteLine ( ex . Message ) ;
118
+ System . Console . ForegroundColor = color ;
119
+ }
81
120
82
121
private static string ShortenDir ( string path )
83
122
{
0 commit comments