comparison MetroWpf/MetroWpf.Framework/Extensions/FileInfoExtensions.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
5 namespace MetroWpf
6 {
7 /// <summary>
8 /// Extension methods for the FileInfo and FileInfo-Array classes
9 /// </summary>
10 public static class FileInfoExtensions
11 {
12 #region · Extensions ·
13
14 /// <summary>
15 /// Renames a file.
16 /// </summary>
17 /// <param name="file">The file.</param>
18 /// <param name="newName">The new name.</param>
19 /// <returns>The renamed file</returns>
20 /// <example>
21 /// <code>
22 /// var file = new FileInfo(@"c:\test.txt");
23 /// file.Rename("test2.txt");
24 /// </code></example>
25 public static FileInfo Rename(this FileInfo file, string newName)
26 {
27 var filePath = Path.Combine(Path.GetDirectoryName(file.FullName), newName);
28
29 file.MoveTo(filePath);
30
31 return file;
32 }
33
34 /// <summary>
35 /// Renames a without changing its extension.
36 /// </summary>
37 /// <param name="file">The file.</param>
38 /// <param name="newName">The new name.</param>
39 /// <returns>The renamed file</returns>
40 /// <example>
41 /// <code>
42 /// var file = new FileInfo(@"c:\test.txt");
43 /// file.RenameFileWithoutExtension("test3");
44 /// </code></example>
45 public static FileInfo RenameFileWithoutExtension(this FileInfo file, string newName)
46 {
47 var fileName = string.Concat(newName, file.Extension);
48
49 file.Rename(fileName);
50
51 return file;
52 }
53
54 /// <summary>
55 /// Changes the files extension.
56 /// </summary>
57 /// <param name="file">The file.</param>
58 /// <param name="newExtension">The new extension.</param>
59 /// <returns>The renamed file</returns>
60 /// <example>
61 /// <code>
62 /// var file = new FileInfo(@"c:\test.txt");
63 /// file.ChangeExtension("xml");
64 /// </code></example>
65 public static FileInfo ChangeExtension(this FileInfo file, string newExtension)
66 {
67 newExtension = newExtension.EnsureStartsWith(".");
68
69 var fileName = string.Concat(Path.GetFileNameWithoutExtension(file.FullName), newExtension);
70
71 file.Rename(fileName);
72
73 return file;
74 }
75
76 /// <summary>
77 /// Changes the extensions of several files at once.
78 /// </summary>
79 /// <param name="files">The files.</param>
80 /// <param name="newExtension">The new extension.</param>
81 /// <returns>The renamed files</returns>
82 /// <example>
83 /// <code>
84 /// var files = directory.GetFiles("*.txt", "*.xml");
85 /// files.ChangeExtensions("tmp");
86 /// </code></example>
87 public static FileInfo[] ChangeExtensions(this FileInfo[] files, string newExtension)
88 {
89 files.ForEach(f => f.ChangeExtension(newExtension));
90
91 return files;
92 }
93
94 /// <summary>
95 /// Deletes several files at once and consolidates any exceptions.
96 /// </summary>
97 /// <param name="files">The files.</param>
98 /// <example>
99 /// <code>
100 /// var files = directory.GetFiles("*.txt", "*.xml");
101 /// files.Delete()
102 /// </code></example>
103 public static void Delete(this FileInfo[] files)
104 {
105 files.Delete(true);
106 }
107
108 /// <summary>
109 /// Deletes several files at once and optionally consolidates any exceptions.
110 /// </summary>
111 /// <param name="files">The files.</param>
112 /// <param name="consolidateExceptions">if set to <c>true</c> exceptions are consolidated and the processing is not interrupted.</param>
113 /// <example>
114 /// <code>
115 /// var files = directory.GetFiles("*.txt", "*.xml");
116 /// files.Delete()
117 /// </code></example>
118 public static void Delete(this FileInfo[] files, bool consolidateExceptions)
119 {
120 List<Exception> exceptions = null;
121
122 foreach (var file in files)
123 {
124 try
125 {
126 file.Delete();
127 }
128 catch (Exception e)
129 {
130 if (consolidateExceptions)
131 {
132 if (exceptions == null)
133 {
134 exceptions = new List<Exception>();
135 }
136
137 exceptions.Add(e);
138 }
139 else
140 {
141 throw;
142 }
143 }
144 }
145
146 if ((exceptions != null) && (exceptions.Count > 0))
147 {
148 throw new CombinedException
149 (
150 "Error while deleting one or several files, see InnerExceptions array for details.",
151 exceptions.ToArray()
152 );
153 }
154 }
155
156 /// <summary>
157 /// Copies several files to a new folder at once and consolidates any exceptions.
158 /// </summary>
159 /// <param name="files">The files.</param>
160 /// <param name="targetPath">The target path.</param>
161 /// <returns>The newly created file copies</returns>
162 /// <example>
163 /// <code>
164 /// var files = directory.GetFiles("*.txt", "*.xml");
165 /// var copiedFiles = files.CopyTo(@"c:\temp\");
166 /// </code></example>
167 public static FileInfo[] CopyTo(this FileInfo[] files, string targetPath)
168 {
169 return files.CopyTo(targetPath, true);
170 }
171
172 /// <summary>
173 /// Copies several files to a new folder at once and optionally consolidates any exceptions.
174 /// </summary>
175 /// <param name="files">The files.</param>
176 /// <param name="targetPath">The target path.</param>
177 /// <param name="consolidateExceptions">if set to <c>true</c> exceptions are consolidated and the processing is not interrupted.</param>
178 /// <returns>The newly created file copies</returns>
179 /// <example>
180 /// <code>
181 /// var files = directory.GetFiles("*.txt", "*.xml");
182 /// var copiedFiles = files.CopyTo(@"c:\temp\");
183 /// </code></example>
184 public static FileInfo[] CopyTo(this FileInfo[] files, string targetPath, bool consolidateExceptions)
185 {
186 var copiedfiles = new List<FileInfo>();
187 List<Exception> exceptions = null;
188
189 foreach (var file in files)
190 {
191 try
192 {
193 var fileName = Path.Combine(targetPath, file.Name);
194
195 copiedfiles.Add(file.CopyTo(fileName));
196 }
197 catch (Exception e)
198 {
199 if (consolidateExceptions)
200 {
201 if (exceptions == null)
202 {
203 exceptions = new List<Exception>();
204 }
205
206 exceptions.Add(e);
207 }
208 else
209 {
210 throw;
211 }
212 }
213 }
214
215 if ((exceptions != null) && (exceptions.Count > 0))
216 {
217 throw new CombinedException
218 (
219 "Error while copying one or several files, see InnerExceptions array for details.",
220 exceptions.ToArray()
221 );
222 }
223
224 return copiedfiles.ToArray();
225 }
226
227 /// <summary>
228 /// Moves several files to a new folder at once and optionally consolidates any exceptions.
229 /// </summary>
230 /// <param name="files">The files.</param>
231 /// <param name="targetPath">The target path.</param>
232 /// <returns>The moved files</returns>
233 /// <example>
234 /// <code>
235 /// var files = directory.GetFiles("*.txt", "*.xml");
236 /// files.MoveTo(@"c:\temp\");
237 /// </code></example>
238 public static FileInfo[] MoveTo(this FileInfo[] files, string targetPath)
239 {
240 return files.MoveTo(targetPath, true);
241 }
242
243 /// <summary>
244 /// Movies several files to a new folder at once and optionally consolidates any exceptions.
245 /// </summary>
246 /// <param name="files">The files.</param>
247 /// <param name="targetPath">The target path.</param>
248 /// <param name="consolidateExceptions">if set to <c>true</c> exceptions are consolidated and the processing is not interrupted.</param>
249 /// <returns>The moved files</returns>
250 /// <example>
251 /// <code>
252 /// var files = directory.GetFiles("*.txt", "*.xml");
253 /// files.MoveTo(@"c:\temp\");
254 /// </code></example>
255 public static FileInfo[] MoveTo(this FileInfo[] files, string targetPath, bool consolidateExceptions)
256 {
257 List<Exception> exceptions = null;
258
259 foreach (var file in files)
260 {
261 try
262 {
263 var fileName = Path.Combine(targetPath, file.Name);
264
265 file.MoveTo(fileName);
266 }
267 catch (Exception e)
268 {
269 if (consolidateExceptions)
270 {
271 if (exceptions == null)
272 {
273 exceptions = new List<Exception>();
274 }
275
276 exceptions.Add(e);
277 }
278 else
279 {
280 throw;
281 }
282 }
283 }
284
285 if ((exceptions != null) && (exceptions.Count > 0))
286 {
287 throw new CombinedException
288 (
289 "Error while moving one or several files, see InnerExceptions array for details.",
290 exceptions.ToArray()
291 );
292 }
293
294 return files;
295 }
296
297 /// <summary>
298 /// Sets file attributes for several files at once
299 /// </summary>
300 /// <param name="files">The files.</param>
301 /// <param name="attributes">The attributes to be set.</param>
302 /// <returns>The changed files</returns>
303 /// <example>
304 /// <code>
305 /// var files = directory.GetFiles("*.txt", "*.xml");
306 /// files.SetAttributes(FileAttributes.Archive);
307 /// </code></example>
308 public static FileInfo[] SetAttributes(this FileInfo[] files, FileAttributes attributes)
309 {
310 foreach (var file in files)
311 {
312 file.Attributes = attributes;
313 }
314
315 return files;
316 }
317
318 /// <summary>
319 /// Appends file attributes for several files at once (additive to any existing attributes)
320 /// </summary>
321 /// <param name="files">The files.</param>
322 /// <param name="attributes">The attributes to be set.</param>
323 /// <returns>The changed files</returns>
324 /// <example>
325 /// <code>
326 /// var files = directory.GetFiles("*.txt", "*.xml");
327 /// files.SetAttributesAdditive(FileAttributes.Archive);
328 /// </code></example>
329 public static FileInfo[] SetAttributesAdditive(this FileInfo[] files, FileAttributes attributes)
330 {
331 foreach (var file in files)
332 {
333 file.Attributes = (file.Attributes | attributes);
334 }
335
336 return files;
337 }
338
339 #endregion
340 }
341 }