23
23
24
24
namespace TestUtilities {
25
25
public static class FileUtils {
26
+ public static IEnumerable < string > EnumerateDirectories (
27
+ string root ,
28
+ bool recurse = true ,
29
+ bool fullPaths = true
30
+ ) {
31
+ var queue = new Queue < string > ( ) ;
32
+ if ( ! root . EndsWith ( "\\ " ) ) {
33
+ root += "\\ " ;
34
+ }
35
+ queue . Enqueue ( root ) ;
36
+
37
+ while ( queue . Any ( ) ) {
38
+ var path = queue . Dequeue ( ) ;
39
+ if ( ! path . EndsWith ( "\\ " ) ) {
40
+ path += "\\ " ;
41
+ }
42
+
43
+ IEnumerable < string > dirs = null ;
44
+ try {
45
+ dirs = Directory . GetDirectories ( path ) ;
46
+ } catch ( UnauthorizedAccessException ) {
47
+ } catch ( IOException ) {
48
+ }
49
+ if ( dirs == null ) {
50
+ continue ;
51
+ }
52
+
53
+ foreach ( var d in dirs ) {
54
+ if ( ! fullPaths && ! d . StartsWith ( root , StringComparison . OrdinalIgnoreCase ) ) {
55
+ continue ;
56
+ }
57
+ if ( recurse ) {
58
+ queue . Enqueue ( d ) ;
59
+ }
60
+ yield return fullPaths ? d : d . Substring ( root . Length ) ;
61
+ }
62
+ }
63
+ }
64
+
65
+ public static IEnumerable < string > EnumerateFiles (
66
+ string root ,
67
+ string pattern = "*" ,
68
+ bool recurse = true ,
69
+ bool fullPaths = true
70
+ ) {
71
+ if ( ! root . EndsWith ( "\\ " ) ) {
72
+ root += "\\ " ;
73
+ }
74
+
75
+ foreach ( var dir in Enumerable . Repeat ( root , 1 ) . Concat ( EnumerateDirectories ( root , recurse , fullPaths ) ) ) {
76
+ var fullDir = ( fullPaths || Path . IsPathRooted ( dir ) ) ? dir : ( root + dir ) ;
77
+
78
+ IEnumerable < string > files = null ;
79
+ try {
80
+ files = Directory . GetFiles ( fullDir , pattern ) ;
81
+ } catch ( UnauthorizedAccessException ) {
82
+ } catch ( IOException ) {
83
+ }
84
+ if ( files == null ) {
85
+ continue ;
86
+ }
87
+
88
+ foreach ( var f in files ) {
89
+ if ( ! fullPaths && ! f . StartsWith ( root , StringComparison . OrdinalIgnoreCase ) ) {
90
+ continue ;
91
+ }
92
+ yield return fullPaths ? f : f . Substring ( root . Length ) ;
93
+ }
94
+ }
95
+ }
96
+
26
97
public static void CopyDirectory ( string sourceDir , string destDir ) {
27
98
sourceDir = sourceDir . TrimEnd ( '\\ ' ) ;
28
99
destDir = destDir . TrimEnd ( '\\ ' ) ;
@@ -31,12 +102,8 @@ public static void CopyDirectory(string sourceDir, string destDir) {
31
102
} catch ( IOException ) {
32
103
}
33
104
34
- var newDirectories = new HashSet < string > ( from d in Directory . EnumerateDirectories ( sourceDir , "*" , SearchOption . AllDirectories )
35
- where d . StartsWith ( sourceDir )
36
- select d . Substring ( sourceDir . Length + 1 ) , StringComparer . OrdinalIgnoreCase ) ;
37
- newDirectories . ExceptWith ( from d in Directory . EnumerateDirectories ( destDir , "*" , SearchOption . AllDirectories )
38
- where d . StartsWith ( destDir )
39
- select d . Substring ( destDir . Length + 1 ) ) ;
105
+ var newDirectories = new HashSet < string > ( EnumerateDirectories ( sourceDir , fullPaths : false ) , StringComparer . OrdinalIgnoreCase ) ;
106
+ newDirectories . ExceptWith ( EnumerateDirectories ( destDir , fullPaths : false ) ) ;
40
107
41
108
foreach ( var newDir in newDirectories . OrderBy ( i => i . Length ) . Select ( i => Path . Combine ( destDir , i ) ) ) {
42
109
try {
@@ -46,12 +113,8 @@ where d.StartsWith(destDir)
46
113
}
47
114
}
48
115
49
- var newFiles = new HashSet < string > ( from f in Directory . EnumerateFiles ( sourceDir , "*" , SearchOption . AllDirectories )
50
- where f . StartsWith ( sourceDir )
51
- select f . Substring ( sourceDir . Length + 1 ) , StringComparer . OrdinalIgnoreCase ) ;
52
- newFiles . ExceptWith ( from f in Directory . EnumerateFiles ( destDir , "*" , SearchOption . AllDirectories )
53
- where f . StartsWith ( destDir )
54
- select f . Substring ( destDir . Length + 1 ) ) ;
116
+ var newFiles = new HashSet < string > ( EnumerateFiles ( sourceDir , fullPaths : false ) , StringComparer . OrdinalIgnoreCase ) ;
117
+ newFiles . ExceptWith ( EnumerateFiles ( destDir , fullPaths : false ) ) ;
55
118
56
119
foreach ( var newFile in newFiles ) {
57
120
var copyFrom = Path . Combine ( sourceDir , newFile ) ;
0 commit comments