Skip to content

Commit 46fa408

Browse files
committed
Added new methods in FileExtensions
1 parent 288a4b9 commit 46fa408

File tree

3 files changed

+153
-11
lines changed

3 files changed

+153
-11
lines changed

common/CommonAssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
[assembly: AssemblyTrademark("")]
88
[assembly: AssemblyCulture("")]
99

10-
[assembly: AssemblyVersion("1.0.9.0")]
11-
[assembly: AssemblyFileVersion("1.0.9.0")]
10+
[assembly: AssemblyVersion("1.0.9.1")]
11+
[assembly: AssemblyFileVersion("1.0.9.1")]

common/PCLExt.FileStorage.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
33
<metadata>
44
<id>PCLExt.FileStorage</id>
5-
<version>1.0.9</version>
5+
<version>1.0.9.1</version>
66
<title>PCL Extension - File Storage API</title>
77
<authors>Daniel Plaisted,Aragas</authors>
88
<owners>Aragas</owners>

src/PCLExt.FileStorage.Abstractions/FileExtensions.cs

Lines changed: 150 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// Which is released under the MS-PL license.
88
//-----------------------------------------------------------------------
99

10+
using System.Collections.Generic;
1011
using System.IO;
1112
using System.Threading.Tasks;
1213

@@ -17,6 +18,18 @@ namespace PCLExt.FileStorage
1718
/// </summary>
1819
public static class FileExtensions
1920
{
21+
/// <summary>
22+
/// Reads the contents of a file as a string
23+
/// </summary>
24+
/// <param name="file">The file to read </param>
25+
/// <returns>The contents of the file</returns>
26+
public static string ReadAllText(this IFile file)
27+
{
28+
using (var stream = file.Open(FileAccess.Read))
29+
using (var sr = new StreamReader(stream))
30+
return sr.ReadToEnd();
31+
}
32+
2033
/// <summary>
2134
/// Reads the contents of a file as a string
2235
/// </summary>
@@ -26,9 +39,22 @@ public static async Task<string> ReadAllTextAsync(this IFile file)
2639
{
2740
using (var stream = await file.OpenAsync(FileAccess.Read).ConfigureAwait(false))
2841
using (var sr = new StreamReader(stream))
42+
return await sr.ReadToEndAsync().ConfigureAwait(false);
43+
}
44+
45+
/// <summary>
46+
/// Writes text to a file, overwriting any existing data
47+
/// </summary>
48+
/// <param name="file">The file to write to</param>
49+
/// <param name="contents">The content to write to the file</param>
50+
/// <returns>A task which completes when the write operation finishes</returns>
51+
public static void WriteAllText(this IFile file, string contents)
52+
{
53+
using (var stream = file.Open(FileAccess.ReadAndWrite))
2954
{
30-
var text = await sr.ReadToEndAsync().ConfigureAwait(false);
31-
return text;
55+
stream.SetLength(0);
56+
using (var sw = new StreamWriter(stream))
57+
sw.Write(contents);
3258
}
3359
}
3460

@@ -48,19 +74,135 @@ public static async Task WriteAllTextAsync(this IFile file, string contents)
4874
}
4975
}
5076

77+
5178
/// <summary>
52-
/// Appends lines to a file, and then closes the file.
79+
///
5380
/// </summary>
54-
/// <param name="file">The file to write to</param>
55-
/// <param name="contents">The content to write to the file</param>
56-
/// <returns>A task which completes when the write operation finishes</returns>
57-
public static async Task AppendAllLinesAsync(this IFile file, string contents)
81+
/// <param name="file"></param>
82+
/// <returns></returns>
83+
public static string[] ReadAllLines(this IFile file)
84+
{
85+
using (var stream = file.Open(FileAccess.Read))
86+
using (var sr = new StreamReader(stream))
87+
{
88+
var lines = new List<string>();
89+
while (!sr.EndOfStream)
90+
lines.Add(sr.ReadLine());
91+
return lines.ToArray();
92+
}
93+
}
94+
95+
/// <summary>
96+
///
97+
/// </summary>
98+
/// <param name="file"></param>
99+
/// <returns></returns>
100+
public static async Task<string[]> ReadAllLinesAsync(this IFile file)
101+
{
102+
using (var stream = await file.OpenAsync(FileAccess.Read).ConfigureAwait(false))
103+
using (var sr = new StreamReader(stream))
104+
{
105+
var lines = new List<string>();
106+
while (!sr.EndOfStream)
107+
lines.Add(await sr.ReadLineAsync().ConfigureAwait(false));
108+
return lines.ToArray();
109+
}
110+
}
111+
112+
/// <summary>
113+
///
114+
/// </summary>
115+
/// <param name="file"></param>
116+
/// <param name="lines"></param>
117+
public static void WriteAllLines(this IFile file, IEnumerable<string> lines)
118+
{
119+
using (var stream = file.Open(FileAccess.ReadAndWrite))
120+
{
121+
stream.SetLength(0);
122+
using (var sw = new StreamWriter(stream))
123+
foreach (var line in lines)
124+
sw.WriteLine(line);
125+
}
126+
}
127+
128+
/// <summary>
129+
///
130+
/// </summary>
131+
/// <param name="file"></param>
132+
/// <param name="lines"></param>
133+
public static async Task WriteAllLinesAsync(this IFile file, IEnumerable<string> lines)
134+
{
135+
using (var stream = await file.OpenAsync(FileAccess.ReadAndWrite).ConfigureAwait(false))
136+
{
137+
stream.SetLength(0);
138+
using (var sw = new StreamWriter(stream))
139+
foreach (var line in lines)
140+
await sw.WriteLineAsync(line).ConfigureAwait(false);
141+
}
142+
}
143+
144+
145+
/// <summary>
146+
///
147+
/// </summary>
148+
/// <param name="file"></param>
149+
/// <param name="contents"></param>
150+
public static void AppendText(this IFile file, string contents)
151+
{
152+
using (var stream = file.Open(FileAccess.ReadAndWrite))
153+
{
154+
stream.Seek(stream.Length, SeekOrigin.Begin);
155+
using (var sw = new StreamWriter(stream))
156+
sw.Write(contents);
157+
}
158+
}
159+
160+
/// <summary>
161+
///
162+
/// </summary>
163+
/// <param name="file"></param>
164+
/// <param name="contents"></param>
165+
/// <returns></returns>
166+
public static async Task AppendTextAsync(this IFile file, string contents)
167+
{
168+
using (var stream = await file.OpenAsync(FileAccess.ReadAndWrite).ConfigureAwait(false))
169+
{
170+
stream.Seek(stream.Length, SeekOrigin.Begin);
171+
using (var sw = new StreamWriter(stream))
172+
await sw.WriteAsync(contents).ConfigureAwait(false);
173+
}
174+
}
175+
176+
/// <summary>
177+
///
178+
/// </summary>
179+
/// <param name="file"></param>
180+
/// <param name="lines"></param>
181+
public static void AppendLines(this IFile file, IEnumerable<string> lines)
182+
{
183+
using (var stream = file.Open(FileAccess.ReadAndWrite))
184+
{
185+
stream.Seek(stream.Length, SeekOrigin.Begin);
186+
using (var sw = new StreamWriter(stream))
187+
foreach (var line in lines)
188+
sw.WriteLine(line);
189+
}
190+
}
191+
192+
/// <summary>
193+
///
194+
/// </summary>
195+
/// <param name="file"></param>
196+
/// <param name="lines"></param>
197+
/// <returns></returns>
198+
public static async Task AppendLinesAsync(this IFile file, IEnumerable<string> lines)
58199
{
59200
using (var stream = await file.OpenAsync(FileAccess.ReadAndWrite).ConfigureAwait(false))
60201
{
61202
stream.Seek(stream.Length, SeekOrigin.Begin);
62203
using (var sw = new StreamWriter(stream))
63-
await sw.WriteLineAsync(contents).ConfigureAwait(false);
204+
foreach (var line in lines)
205+
await sw.WriteLineAsync(line).ConfigureAwait(false);
64206
}
65207
}
66208
}

0 commit comments

Comments
 (0)