Skip to content

Commit

Permalink
Added new methods in FileExtensions
Browse files Browse the repository at this point in the history
  • Loading branch information
Aragas committed Mar 30, 2017
1 parent 288a4b9 commit 46fa408
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 11 deletions.
4 changes: 2 additions & 2 deletions common/CommonAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
2 changes: 1 addition & 1 deletion common/PCLExt.FileStorage.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>PCLExt.FileStorage</id>
<version>1.0.9</version>
<version>1.0.9.1</version>
<title>PCL Extension - File Storage API</title>
<authors>Daniel Plaisted,Aragas</authors>
<owners>Aragas</owners>
Expand Down
158 changes: 150 additions & 8 deletions src/PCLExt.FileStorage.Abstractions/FileExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// Which is released under the MS-PL license.
//-----------------------------------------------------------------------

using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

Expand All @@ -17,6 +18,18 @@ namespace PCLExt.FileStorage
/// </summary>
public static class FileExtensions
{
/// <summary>
/// Reads the contents of a file as a string
/// </summary>
/// <param name="file">The file to read </param>
/// <returns>The contents of the file</returns>
public static string ReadAllText(this IFile file)
{
using (var stream = file.Open(FileAccess.Read))
using (var sr = new StreamReader(stream))
return sr.ReadToEnd();
}

/// <summary>
/// Reads the contents of a file as a string
/// </summary>
Expand All @@ -26,9 +39,22 @@ public static async Task<string> 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);
}

/// <summary>
/// Writes text to a file, overwriting any existing data
/// </summary>
/// <param name="file">The file to write to</param>
/// <param name="contents">The content to write to the file</param>
/// <returns>A task which completes when the write operation finishes</returns>
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);
}
}

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


/// <summary>
/// Appends lines to a file, and then closes the file.
///
/// </summary>
/// <param name="file">The file to write to</param>
/// <param name="contents">The content to write to the file</param>
/// <returns>A task which completes when the write operation finishes</returns>
public static async Task AppendAllLinesAsync(this IFile file, string contents)
/// <param name="file"></param>
/// <returns></returns>
public static string[] ReadAllLines(this IFile file)
{
using (var stream = file.Open(FileAccess.Read))
using (var sr = new StreamReader(stream))
{
var lines = new List<string>();
while (!sr.EndOfStream)
lines.Add(sr.ReadLine());
return lines.ToArray();
}
}

/// <summary>
///
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
public static async Task<string[]> ReadAllLinesAsync(this IFile file)
{
using (var stream = await file.OpenAsync(FileAccess.Read).ConfigureAwait(false))
using (var sr = new StreamReader(stream))
{
var lines = new List<string>();
while (!sr.EndOfStream)
lines.Add(await sr.ReadLineAsync().ConfigureAwait(false));
return lines.ToArray();
}
}

/// <summary>
///
/// </summary>
/// <param name="file"></param>
/// <param name="lines"></param>
public static void WriteAllLines(this IFile file, IEnumerable<string> 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);
}
}

/// <summary>
///
/// </summary>
/// <param name="file"></param>
/// <param name="lines"></param>
public static async Task WriteAllLinesAsync(this IFile file, IEnumerable<string> 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);
}
}


/// <summary>
///
/// </summary>
/// <param name="file"></param>
/// <param name="contents"></param>
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);
}
}

/// <summary>
///
/// </summary>
/// <param name="file"></param>
/// <param name="contents"></param>
/// <returns></returns>
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);
}
}

/// <summary>
///
/// </summary>
/// <param name="file"></param>
/// <param name="lines"></param>
public static void AppendLines(this IFile file, IEnumerable<string> 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);
}
}

/// <summary>
///
/// </summary>
/// <param name="file"></param>
/// <param name="lines"></param>
/// <returns></returns>
public static async Task AppendLinesAsync(this IFile file, IEnumerable<string> 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);
}
}
}
Expand Down

0 comments on commit 46fa408

Please sign in to comment.