From 46fa408d342c92e44e8b2fb0c684c0d43ef9c9d9 Mon Sep 17 00:00:00 2001 From: Aragas Date: Thu, 30 Mar 2017 14:04:37 +0300 Subject: [PATCH] Added new methods in FileExtensions --- common/CommonAssemblyInfo.cs | 4 +- common/PCLExt.FileStorage.nuspec | 2 +- .../FileExtensions.cs | 158 +++++++++++++++++- 3 files changed, 153 insertions(+), 11 deletions(-) diff --git a/common/CommonAssemblyInfo.cs b/common/CommonAssemblyInfo.cs index d79b9ab..019a316 100644 --- a/common/CommonAssemblyInfo.cs +++ b/common/CommonAssemblyInfo.cs @@ -7,5 +7,5 @@ [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] -[assembly: AssemblyVersion("1.0.9.0")] -[assembly: AssemblyFileVersion("1.0.9.0")] +[assembly: AssemblyVersion("1.0.9.1")] +[assembly: AssemblyFileVersion("1.0.9.1")] diff --git a/common/PCLExt.FileStorage.nuspec b/common/PCLExt.FileStorage.nuspec index 988f079..a464d51 100644 --- a/common/PCLExt.FileStorage.nuspec +++ b/common/PCLExt.FileStorage.nuspec @@ -2,7 +2,7 @@ PCLExt.FileStorage - 1.0.9 + 1.0.9.1 PCL Extension - File Storage API Daniel Plaisted,Aragas Aragas diff --git a/src/PCLExt.FileStorage.Abstractions/FileExtensions.cs b/src/PCLExt.FileStorage.Abstractions/FileExtensions.cs index 9bd3d9f..a5ba658 100644 --- a/src/PCLExt.FileStorage.Abstractions/FileExtensions.cs +++ b/src/PCLExt.FileStorage.Abstractions/FileExtensions.cs @@ -7,6 +7,7 @@ // Which is released under the MS-PL license. //----------------------------------------------------------------------- +using System.Collections.Generic; using System.IO; using System.Threading.Tasks; @@ -17,6 +18,18 @@ namespace PCLExt.FileStorage /// public static class FileExtensions { + /// + /// Reads the contents of a file as a string + /// + /// The file to read + /// The contents of the file + public static string ReadAllText(this IFile file) + { + using (var stream = file.Open(FileAccess.Read)) + using (var sr = new StreamReader(stream)) + return sr.ReadToEnd(); + } + /// /// Reads the contents of a file as a string /// @@ -26,9 +39,22 @@ public static async Task ReadAllTextAsync(this IFile file) { using (var stream = await file.OpenAsync(FileAccess.Read).ConfigureAwait(false)) using (var sr = new StreamReader(stream)) + return await sr.ReadToEndAsync().ConfigureAwait(false); + } + + /// + /// Writes text to a file, overwriting any existing data + /// + /// The file to write to + /// The content to write to the file + /// A task which completes when the write operation finishes + public static void WriteAllText(this IFile file, string contents) + { + using (var stream = file.Open(FileAccess.ReadAndWrite)) { - var text = await sr.ReadToEndAsync().ConfigureAwait(false); - return text; + stream.SetLength(0); + using (var sw = new StreamWriter(stream)) + sw.Write(contents); } } @@ -48,19 +74,135 @@ public static async Task WriteAllTextAsync(this IFile file, string contents) } } + /// - /// Appends lines to a file, and then closes the file. + /// /// - /// The file to write to - /// The content to write to the file - /// A task which completes when the write operation finishes - public static async Task AppendAllLinesAsync(this IFile file, string contents) + /// + /// + public static string[] ReadAllLines(this IFile file) + { + using (var stream = file.Open(FileAccess.Read)) + using (var sr = new StreamReader(stream)) + { + var lines = new List(); + while (!sr.EndOfStream) + lines.Add(sr.ReadLine()); + return lines.ToArray(); + } + } + + /// + /// + /// + /// + /// + public static async Task ReadAllLinesAsync(this IFile file) + { + using (var stream = await file.OpenAsync(FileAccess.Read).ConfigureAwait(false)) + using (var sr = new StreamReader(stream)) + { + var lines = new List(); + while (!sr.EndOfStream) + lines.Add(await sr.ReadLineAsync().ConfigureAwait(false)); + return lines.ToArray(); + } + } + + /// + /// + /// + /// + /// + public static void WriteAllLines(this IFile file, IEnumerable lines) + { + using (var stream = file.Open(FileAccess.ReadAndWrite)) + { + stream.SetLength(0); + using (var sw = new StreamWriter(stream)) + foreach (var line in lines) + sw.WriteLine(line); + } + } + + /// + /// + /// + /// + /// + public static async Task WriteAllLinesAsync(this IFile file, IEnumerable lines) + { + using (var stream = await file.OpenAsync(FileAccess.ReadAndWrite).ConfigureAwait(false)) + { + stream.SetLength(0); + using (var sw = new StreamWriter(stream)) + foreach (var line in lines) + await sw.WriteLineAsync(line).ConfigureAwait(false); + } + } + + + /// + /// + /// + /// + /// + public static void AppendText(this IFile file, string contents) + { + using (var stream = file.Open(FileAccess.ReadAndWrite)) + { + stream.Seek(stream.Length, SeekOrigin.Begin); + using (var sw = new StreamWriter(stream)) + sw.Write(contents); + } + } + + /// + /// + /// + /// + /// + /// + public static async Task AppendTextAsync(this IFile file, string contents) + { + using (var stream = await file.OpenAsync(FileAccess.ReadAndWrite).ConfigureAwait(false)) + { + stream.Seek(stream.Length, SeekOrigin.Begin); + using (var sw = new StreamWriter(stream)) + await sw.WriteAsync(contents).ConfigureAwait(false); + } + } + + /// + /// + /// + /// + /// + public static void AppendLines(this IFile file, IEnumerable lines) + { + using (var stream = file.Open(FileAccess.ReadAndWrite)) + { + stream.Seek(stream.Length, SeekOrigin.Begin); + using (var sw = new StreamWriter(stream)) + foreach (var line in lines) + sw.WriteLine(line); + } + } + + /// + /// + /// + /// + /// + /// + public static async Task AppendLinesAsync(this IFile file, IEnumerable lines) { using (var stream = await file.OpenAsync(FileAccess.ReadAndWrite).ConfigureAwait(false)) { stream.Seek(stream.Length, SeekOrigin.Begin); using (var sw = new StreamWriter(stream)) - await sw.WriteLineAsync(contents).ConfigureAwait(false); + foreach (var line in lines) + await sw.WriteLineAsync(line).ConfigureAwait(false); } } }