Mercurial > silverbladetech
comparison MetroWpf/MetroWpf.Framework/Extensions/DirectoryInfoExtensions.cs @ 15:060f02cd4591
Initial commit, pre airport work
author | stevenh7776 stevenhollidge@hotmail.com |
---|---|
date | Mon, 12 Mar 2012 23:05:21 +0800 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
14:741981715d94 | 15:060f02cd4591 |
---|---|
1 using System; | |
2 using System.Collections.Generic; | |
3 using System.IO; | |
4 using System.Linq; | |
5 | |
6 namespace MetroWpf | |
7 { | |
8 /// <summary> | |
9 /// Extension methods for the DirectoryInfo class | |
10 /// </summary> | |
11 public static class DirectoryInfoExtensions | |
12 { | |
13 #region · Extensions · | |
14 | |
15 /// <summary> | |
16 /// Gets all files in the directory matching one of the several (!) supplied patterns (instead of just one in the regular implementation). | |
17 /// </summary> | |
18 /// <param name="directory">The directory.</param> | |
19 /// <param name="patterns">The patterns.</param> | |
20 /// <returns>The matching files</returns> | |
21 /// <remarks>This methods is quite perfect to be used in conjunction with the newly created FileInfo-Array extension methods.</remarks> | |
22 /// <example> | |
23 /// <code> | |
24 /// var files = directory.GetFiles("*.txt", "*.xml"); | |
25 /// </code></example> | |
26 public static FileInfo[] GetFiles(this DirectoryInfo directory, params string[] patterns) | |
27 { | |
28 var files = new List<FileInfo>(); | |
29 | |
30 foreach (var pattern in patterns) | |
31 { | |
32 files.AddRange(directory.GetFiles(pattern)); | |
33 } | |
34 | |
35 return files.ToArray(); | |
36 } | |
37 | |
38 /// <summary> | |
39 /// Searches the provided directory recursively and returns the first file matching the provided pattern. | |
40 /// </summary> | |
41 /// <param name="directory">The directory.</param> | |
42 /// <param name="pattern">The pattern.</param> | |
43 /// <returns>The found file</returns> | |
44 /// <example> | |
45 /// <code> | |
46 /// var directory = new DirectoryInfo(@"c:\"); | |
47 /// var file = directory.FindFileRecursive("win.ini"); | |
48 /// </code></example> | |
49 public static FileInfo FindFileRecursive(this DirectoryInfo directory, string pattern) | |
50 { | |
51 var files = directory.GetFiles(pattern); | |
52 | |
53 if (files.Length > 0) | |
54 { | |
55 return files[0]; | |
56 } | |
57 | |
58 foreach (var subDirectory in directory.GetDirectories()) | |
59 { | |
60 var foundFile = subDirectory.FindFileRecursive(pattern); | |
61 | |
62 if (foundFile != null) | |
63 { | |
64 return foundFile; | |
65 } | |
66 } | |
67 | |
68 return null; | |
69 } | |
70 | |
71 /// <summary> | |
72 /// Searches the provided directory recursively and returns the first file matching to the provided predicate. | |
73 /// </summary> | |
74 /// <param name="directory">The directory.</param> | |
75 /// <param name="predicate">The predicate.</param> | |
76 /// <returns>The found file</returns> | |
77 /// <example> | |
78 /// <code> | |
79 /// var directory = new DirectoryInfo(@"c:\"); | |
80 /// var file = directory.FindFileRecursive(f => f.Extension == ".ini"); | |
81 /// </code></example> | |
82 public static FileInfo FindFileRecursive(this DirectoryInfo directory, Func<FileInfo, bool> predicate) | |
83 { | |
84 foreach (var file in directory.GetFiles()) | |
85 { | |
86 if (predicate(file)) | |
87 { | |
88 return file; | |
89 } | |
90 } | |
91 | |
92 foreach (var subDirectory in directory.GetDirectories()) | |
93 { | |
94 var foundFile = subDirectory.FindFileRecursive(predicate); | |
95 | |
96 if (foundFile != null) | |
97 { | |
98 return foundFile; | |
99 } | |
100 } | |
101 | |
102 return null; | |
103 } | |
104 | |
105 /// <summary> | |
106 /// Searches the provided directory recursively and returns the all files matching the provided pattern. | |
107 /// </summary> | |
108 /// <param name="directory">The directory.</param> | |
109 /// <param name="pattern">The pattern.</param> | |
110 /// <remarks>This methods is quite perfect to be used in conjunction with the newly created FileInfo-Array extension methods.</remarks> | |
111 /// <returns>The found files</returns> | |
112 /// <example> | |
113 /// <code> | |
114 /// var directory = new DirectoryInfo(@"c:\"); | |
115 /// var files = directory.FindFilesRecursive("*.ini"); | |
116 /// </code></example> | |
117 public static FileInfo[] FindFilesRecursive(this DirectoryInfo directory, string pattern) | |
118 { | |
119 var foundFiles = new List<FileInfo>(); | |
120 | |
121 FindFilesRecursive(directory, pattern, foundFiles); | |
122 | |
123 return foundFiles.ToArray(); | |
124 } | |
125 | |
126 private static void FindFilesRecursive(DirectoryInfo directory, string pattern, List<FileInfo> foundFiles) | |
127 { | |
128 foundFiles.AddRange(directory.GetFiles(pattern)); | |
129 | |
130 directory.GetDirectories().ForEach(d => FindFilesRecursive(d, pattern, foundFiles)); | |
131 } | |
132 | |
133 /// <summary> | |
134 /// Searches the provided directory recursively and returns the all files matching to the provided predicate. | |
135 /// </summary> | |
136 /// <param name="directory">The directory.</param> | |
137 /// <param name="predicate">The predicate.</param> | |
138 /// <returns>The found files</returns> | |
139 /// <remarks>This methods is quite perfect to be used in conjunction with the newly created FileInfo-Array extension methods.</remarks> | |
140 /// <example> | |
141 /// <code> | |
142 /// var directory = new DirectoryInfo(@"c:\"); | |
143 /// var files = directory.FindFilesRecursive(f => f.Extension == ".ini"); | |
144 /// </code></example> | |
145 public static FileInfo[] FindFilesRecursive(this DirectoryInfo directory, Func<FileInfo, bool> predicate) | |
146 { | |
147 var foundFiles = new List<FileInfo>(); | |
148 | |
149 FindFilesRecursive(directory, predicate, foundFiles); | |
150 | |
151 return foundFiles.ToArray(); | |
152 } | |
153 | |
154 private static void FindFilesRecursive(DirectoryInfo directory, Func<FileInfo, bool> predicate, List<FileInfo> foundFiles) | |
155 { | |
156 foundFiles.AddRange(directory.GetFiles().Where(predicate)); | |
157 | |
158 directory.GetDirectories().ForEach(d => FindFilesRecursive(d, predicate, foundFiles)); | |
159 } | |
160 | |
161 #endregion | |
162 } | |
163 } |