|
1 | | -using System.Collections; |
2 | | -using System.Collections.Generic; |
| 1 | +using System.Collections.Generic; |
3 | 2 | using System.Text; |
4 | 3 |
|
5 | 4 | namespace System.IO.Abstractions |
6 | 5 | { |
7 | 6 | public static class IFileInfoExtensions |
8 | 7 | { |
9 | 8 | /// <summary> |
10 | | - /// Throws an exception if the file <paramref name="info"/> doesn't exists |
| 9 | + /// Throws an exception if the <paramref name="file"/> doesn't exists |
11 | 10 | /// </summary> |
12 | | - /// <param name="info">File that will be checked for existance</param> |
| 11 | + /// <param name="file">File that will be checked for existance</param> |
13 | 12 | /// <exception cref="FileNotFoundException">Exception thrown if the file is not found</exception> |
14 | | - public static void ThrowIfNotFound(this IFileInfo info) |
| 13 | + public static void ThrowIfNotFound(this IFileInfo file) |
15 | 14 | { |
16 | | - if (!info.Exists) |
17 | | - throw new FileNotFoundException(StringResources.Format("COULD_NOT_FIND_FILE_EXCEPTION", info.FullName)); |
| 15 | + if (!file.Exists) |
| 16 | + throw new FileNotFoundException(StringResources.Format("COULD_NOT_FIND_FILE_EXCEPTION", file.FullName)); |
18 | 17 | } |
19 | 18 |
|
20 | 19 | /// <summary> |
21 | | - /// Creates an <see cref="IEnumerable{String}"/> that can enumerate the lines of text in the <paramref name="info"/> file |
| 20 | + /// Creates an <see cref="IEnumerable{String}"/> that can enumerate the lines of text in the <paramref name="file"/> |
22 | 21 | /// </summary> |
23 | | - /// <param name="info">File to enumerate content</param> |
| 22 | + /// <param name="file">File to enumerate content</param> |
24 | 23 | /// <returns>Returns an <see cref="IEnumerable{String}"/> to enumerate the content of the file</returns> |
25 | | - public static IEnumerable<string> EnumerateLines(this IFileInfo info) |
| 24 | + public static IEnumerable<string> EnumerateLines(this IFileInfo file) |
26 | 25 | { |
27 | | - return new LineEnumerable(info, null); |
| 26 | + return new LineEnumerable(file, null); |
28 | 27 | } |
29 | 28 |
|
30 | 29 | /// <summary> |
31 | | - /// Creates an <see cref="IEnumerable{String}"/> that can enumerate the lines of text in the <paramref name="info"/> file |
| 30 | + /// Creates an <see cref="IEnumerable{String}"/> that can enumerate the lines of text in the specified <paramref name="file"/> |
32 | 31 | /// using the specified <paramref name="encoding"/> |
33 | 32 | /// </summary> |
34 | | - /// <param name="info">File to enumerate content</param> |
| 33 | + /// <param name="file">File to enumerate content</param> |
35 | 34 | /// <returns>Returns an <see cref="IEnumerable{String}"/> to enumerate the content of the file</returns> |
36 | | - public static IEnumerable<string> EnumerateLines(this IFileInfo info, Encoding encoding) |
| 35 | + public static IEnumerable<string> EnumerateLines(this IFileInfo file, Encoding encoding) |
37 | 36 | { |
38 | | - return new LineEnumerable(info, encoding); |
| 37 | + return new LineEnumerable(file, encoding); |
| 38 | + } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Opens a <see cref="FileSystemStream"/> for the <paramref name="file"/> in the specified <paramref name="mode"/> |
| 42 | + /// </summary> |
| 43 | + /// <param name="file">File to open stream on</param> |
| 44 | + /// <param name="mode">Mode to use when opening the file</param> |
| 45 | + /// <returns>A <see cref="FileSystemStream"/> that can read or write data to the specified <paramref name="file"/></returns> |
| 46 | + public static FileSystemStream OpenFileStream(this IFileInfo file, FileMode mode) |
| 47 | + { |
| 48 | + return file.FileSystem.FileStream.New(file.FullName, mode); |
| 49 | + } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Creates a new empty <paramref name="file"/>. |
| 53 | + /// If the file already exists, the file is truncated. |
| 54 | + /// </summary> |
| 55 | + /// <param name="file">File to create</param> |
| 56 | + /// <returns>The original <see cref="IFileInfo"/> so that methods calls can be chained</returns> |
| 57 | + public static IFileInfo Truncate(this IFileInfo file) |
| 58 | + { |
| 59 | + using(var stream = file.OpenFileStream(FileMode.Create)) |
| 60 | + { |
| 61 | + stream.Dispose(); |
| 62 | + } |
| 63 | + |
| 64 | + return file; |
| 65 | + } |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Writes the specified <paramref name="lines"/> to the specified <paramref name="file"/> using the UTF-8 encoding. |
| 69 | + /// If the file already exists and the <paramref name="overwrite"/> flag is set to true, the file will be truncated. |
| 70 | + /// </summary> |
| 71 | + /// <param name="file">File to write to</param> |
| 72 | + /// <param name="lines">Lines to write to file as text</param> |
| 73 | + /// <param name="overwrite">Flag that specifies if the file can be overwritten if it exists</param> |
| 74 | + /// <exception cref="IOException">Exception thrown if the file already exists and the <paramref name="overwrite"/> flag is set to <see cref="false"/></exception> |
| 75 | + public static void WriteLines(this IFileInfo file, IEnumerable<string> lines, bool overwrite = false) |
| 76 | + { |
| 77 | + using (var stream = file.OpenFileStream(GetWriteFileMode(file, overwrite))) |
| 78 | + using (var writer = new StreamWriter(stream)) |
| 79 | + foreach(var line in lines) |
| 80 | + { |
| 81 | + writer.WriteLine(line); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /// <summary> |
| 86 | + /// Writes the specified <paramref name="lines"/> to the specified <paramref name="file"/> |
| 87 | + /// using the specified <paramref name="encoding"/>. |
| 88 | + /// If the file already exists and the <paramref name="overwrite"/> flag is set to true, the file will be truncated. |
| 89 | + /// </summary> |
| 90 | + /// <param name="file">File to write to</param> |
| 91 | + /// <param name="lines">Lines to write to file as text</param> |
| 92 | + /// <param name="encoding">Encoding to use when writing the <paramref name="lines"/> to the text file</param> |
| 93 | + /// <param name="overwrite">Flag that specifies if the file can be overwritten if it exists</param> |
| 94 | + /// <exception cref="IOException">Exception thrown if the file already exists and the <paramref name="overwrite"/> flag is set to <see cref="false"/></exception> |
| 95 | + public static void WriteLines(this IFileInfo file, IEnumerable<string> lines, Encoding encoding, bool overwrite = false) |
| 96 | + { |
| 97 | + using (var stream = file.OpenFileStream(GetWriteFileMode(file, overwrite))) |
| 98 | + using (var writer = new StreamWriter(stream, encoding)) |
| 99 | + foreach (var line in lines) |
| 100 | + { |
| 101 | + writer.WriteLine(line); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + /// <summary> |
| 106 | + /// Appends the specified <paramref name="lines"/> to the specified <paramref name="file"/> |
| 107 | + /// </summary> |
| 108 | + /// <param name="file">File to append to</param> |
| 109 | + /// <param name="lines">Lines to append to file as text</param> |
| 110 | + public static void AppendLines(this IFileInfo file, IEnumerable<string> lines) |
| 111 | + { |
| 112 | + using (var writer = file.AppendText()) |
| 113 | + foreach (var line in lines) |
| 114 | + { |
| 115 | + writer.WriteLine(line); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private static FileMode GetWriteFileMode(IFileInfo info, bool overwrite) |
| 120 | + { |
| 121 | + if (!overwrite && info.Exists) |
| 122 | + { |
| 123 | + throw new IOException(StringResources.Format("CANNOT_OVERWRITE", info.FullName)); |
| 124 | + } |
| 125 | + |
| 126 | + //if the file already exists it will be truncated |
| 127 | + return FileMode.Create; |
39 | 128 | } |
40 | 129 | } |
41 | 130 | } |
0 commit comments