diff --git a/src/System.IO.Abstractions.Extensions/IFileInfoAsyncExtensions.cs b/src/System.IO.Abstractions.Extensions/IFileInfoAsyncExtensions.cs new file mode 100644 index 0000000..d76b9b2 --- /dev/null +++ b/src/System.IO.Abstractions.Extensions/IFileInfoAsyncExtensions.cs @@ -0,0 +1,98 @@ +#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER +#nullable enable + +using System.Collections.Generic; +using System.Runtime.CompilerServices; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace System.IO.Abstractions +{ + public static class FileInfoFileAsyncExtensions + { + /// + public static async Task AppendAllLinesAsync(this IFileInfo file, IEnumerable contents, CancellationToken cancellationToken = default) + { + await file.FileSystem.File.AppendAllLinesAsync(file.FullName, contents, cancellationToken); + } + + /// + public static async Task AppendAllTextAsync(this IFileInfo file, string? contents, CancellationToken cancellationToken = default) + { + await file.FileSystem.File.AppendAllTextAsync(file.FullName, contents, cancellationToken); + } + + /// + public static async Task ReadAllBytesAsync(this IFileInfo file, CancellationToken cancellationToken = default) + { + return await file.FileSystem.File.ReadAllBytesAsync(file.FullName, cancellationToken); + } + + /// + public static async Task ReadAllLinesAsync(this IFileInfo file, CancellationToken cancellationToken = default) + { + return await file.FileSystem.File.ReadAllLinesAsync(file.FullName, cancellationToken); + } + + /// + public static async Task ReadAllTextAsync(this IFileInfo file, CancellationToken cancellationToken = default) + { + return await file.FileSystem.File.ReadAllTextAsync(file.FullName, cancellationToken); + } + + /// + public static async Task WriteAllBytesAsync(this IFileInfo file, byte[] bytes, CancellationToken cancellationToken = default) + { + await file.FileSystem.File.WriteAllBytesAsync(file.FullName, bytes, cancellationToken); + } + + /// + public static async Task WriteAllLinesAsync(this IFileInfo file, IEnumerable contents, CancellationToken cancellationToken = default) + { + await file.FileSystem.File.WriteAllLinesAsync(file.FullName, contents, cancellationToken); + } + + /// + public static async Task WriteAllTextAsync(this IFileInfo file, string? contents, CancellationToken cancellationToken = default) + { + await file.FileSystem.File.WriteAllTextAsync(file.FullName, contents, cancellationToken); + } + + /// + /// Creates an that can enumerate asynchronously the lines of text in the specified + /// + /// File to enumerate content + /// + /// Returns an to enumerate the content of the file + public static IAsyncEnumerable EnumerateLinesAsync(this IFileInfo file, CancellationToken cancellationToken) + { + return EnumerateLinesAsync(file, null, cancellationToken); + } + + /// + /// Creates an that can enumerate asynchronously the lines of text in the specified + /// using the specified + /// + /// File to enumerate content + /// Encoding to use to read the file + /// + /// Returns an to enumerate the content of the file + public static async IAsyncEnumerable EnumerateLinesAsync(this IFileInfo file, Encoding? encoding, [EnumeratorCancellation] CancellationToken cancellationToken) + { + await using var stream = file.OpenRead(); + using var reader = encoding == null + ? new StreamReader(stream) + : new StreamReader(stream, encoding); + + var line = await reader.ReadLineAsync(); + while (line != null) + { + yield return line; + cancellationToken.ThrowIfCancellationRequested(); + line = await reader.ReadLineAsync(); + } + } + } +} +#endif \ No newline at end of file diff --git a/src/System.IO.Abstractions.Extensions/IFileInfoExtensions.cs b/src/System.IO.Abstractions.Extensions/IFileInfoExtensions.cs index 56b5f4a..0ace295 100644 --- a/src/System.IO.Abstractions.Extensions/IFileInfoExtensions.cs +++ b/src/System.IO.Abstractions.Extensions/IFileInfoExtensions.cs @@ -31,6 +31,7 @@ public static IEnumerable EnumerateLines(this IFileInfo file) /// using the specified /// /// File to enumerate content + /// Encoding to use to read the file /// Returns an to enumerate the content of the file public static IEnumerable EnumerateLines(this IFileInfo file, Encoding encoding) {