diff --git a/src/Spectre.IO/Extensions/IFileExtensions.cs b/src/Spectre.IO/Extensions/IFileExtensions.cs index 8f2b9e0..f573a28 100644 --- a/src/Spectre.IO/Extensions/IFileExtensions.cs +++ b/src/Spectre.IO/Extensions/IFileExtensions.cs @@ -197,7 +197,8 @@ public static async Task ReadAllTextAsync(this IFile file, CancellationT } /// - /// Asynchronously opens a text file, reads all the text in the file, and then closes the file. + /// Asynchronously opens a text file, reads all text in the file with the + /// specified encoding, and then closes the file. /// /// The file to read from. /// The encoding applied to the contents of the file. @@ -267,8 +268,9 @@ public static async Task WriteAllTextAsync( } /// - /// Creates a new file, writes the specified string to the file, and then closes the file. - /// If the target file already exists, it is overwritten. + /// Asynchronously creates a new file, writes the specified string to the file using the + /// specified encoding, and then closes the file. If the target file already exists, + /// it is truncated and overwritten. /// /// The file to write to. /// The string to write to the file. diff --git a/src/Spectre.IO/Extensions/IFileProviderExtensions.cs b/src/Spectre.IO/Extensions/IFileProviderExtensions.cs index 1ccf256..eeed22c 100644 --- a/src/Spectre.IO/Extensions/IFileProviderExtensions.cs +++ b/src/Spectre.IO/Extensions/IFileProviderExtensions.cs @@ -1,5 +1,8 @@ using System; using System.IO; +using System.Text; +using System.Threading; +using System.Threading.Tasks; namespace Spectre.IO; @@ -16,10 +19,8 @@ public static class IFileProviderExtensions /// true if the file exists; otherwise, false. public static bool Exists(this IFileProvider provider, FilePath path) { - if (provider is null) - { - throw new ArgumentNullException(nameof(provider)); - } + ArgumentNullException.ThrowIfNull(provider); + ArgumentNullException.ThrowIfNull(path); var file = provider.Retrieve(path); return file.Exists; @@ -34,10 +35,8 @@ public static bool Exists(this IFileProvider provider, FilePath path) /// The file size in bytes. public static long GetLength(this IFileProvider provider, FilePath path) { - if (provider is null) - { - throw new ArgumentNullException(nameof(provider)); - } + ArgumentNullException.ThrowIfNull(provider); + ArgumentNullException.ThrowIfNull(path); var file = provider.Retrieve(path); return file.Length; @@ -52,10 +51,8 @@ public static long GetLength(this IFileProvider provider, FilePath path) /// The file attributes. public static FileAttributes GetAttributes(this IFileProvider provider, FilePath path) { - if (provider is null) - { - throw new ArgumentNullException(nameof(provider)); - } + ArgumentNullException.ThrowIfNull(provider); + ArgumentNullException.ThrowIfNull(path); var file = provider.Retrieve(path); return file.Attributes; @@ -70,10 +67,8 @@ public static FileAttributes GetAttributes(this IFileProvider provider, FilePath /// The file attributes. public static void SetAttributes(this IFileProvider provider, FilePath path, FileAttributes attributes) { - if (provider is null) - { - throw new ArgumentNullException(nameof(provider)); - } + ArgumentNullException.ThrowIfNull(provider); + ArgumentNullException.ThrowIfNull(path); var file = provider.Retrieve(path); file.Attributes = attributes; @@ -92,10 +87,9 @@ public static void Copy( FilePath destination, bool overwrite) { - if (provider is null) - { - throw new ArgumentNullException(nameof(provider)); - } + ArgumentNullException.ThrowIfNull(provider); + ArgumentNullException.ThrowIfNull(source); + ArgumentNullException.ThrowIfNull(destination); var file = provider.Retrieve(source); file.Copy(destination, overwrite); @@ -112,10 +106,9 @@ public static void CreateSymbolicLink( FilePath source, FilePath destination) { - if (provider is null) - { - throw new ArgumentNullException(nameof(provider)); - } + ArgumentNullException.ThrowIfNull(provider); + ArgumentNullException.ThrowIfNull(source); + ArgumentNullException.ThrowIfNull(destination); var file = provider.Retrieve(source); file.CreateSymbolicLink(destination); @@ -129,6 +122,10 @@ public static void CreateSymbolicLink( /// The destination file path. public static void Move(this IFileProvider provider, FilePath source, FilePath destination) { + ArgumentNullException.ThrowIfNull(provider); + ArgumentNullException.ThrowIfNull(source); + ArgumentNullException.ThrowIfNull(destination); + Move(provider, source, destination, false); } @@ -141,10 +138,9 @@ public static void Move(this IFileProvider provider, FilePath source, FilePath d /// Will overwrite existing destination file if set to true. public static void Move(this IFileProvider provider, FilePath source, FilePath destination, bool overwrite) { - if (provider is null) - { - throw new ArgumentNullException(nameof(provider)); - } + ArgumentNullException.ThrowIfNull(provider); + ArgumentNullException.ThrowIfNull(source); + ArgumentNullException.ThrowIfNull(destination); var file = provider.Retrieve(source); file.Move(destination, overwrite); @@ -157,10 +153,8 @@ public static void Move(this IFileProvider provider, FilePath source, FilePath d /// The file to delete. public static void Delete(this IFileProvider provider, FilePath path) { - if (provider is null) - { - throw new ArgumentNullException(nameof(provider)); - } + ArgumentNullException.ThrowIfNull(provider); + ArgumentNullException.ThrowIfNull(path); var file = provider.Retrieve(path); file.Delete(); @@ -182,10 +176,8 @@ public static Stream Open( FileAccess fileAccess, FileShare fileShare) { - if (provider is null) - { - throw new ArgumentNullException(nameof(provider)); - } + ArgumentNullException.ThrowIfNull(provider); + ArgumentNullException.ThrowIfNull(path); var file = provider.Retrieve(path); return file.Open(fileMode, fileAccess, fileShare); @@ -200,10 +192,8 @@ public static Stream Open( /// A to the file. public static Stream Open(this IFileProvider provider, FilePath path, FileMode mode) { - if (provider == null) - { - throw new ArgumentNullException(nameof(provider)); - } + ArgumentNullException.ThrowIfNull(provider); + ArgumentNullException.ThrowIfNull(path); return provider.Retrieve(path).Open( mode, @@ -225,10 +215,8 @@ public static Stream Open( FileMode mode, FileAccess access) { - if (provider == null) - { - throw new ArgumentNullException(nameof(provider)); - } + ArgumentNullException.ThrowIfNull(provider); + ArgumentNullException.ThrowIfNull(path); return provider.Retrieve(path).Open(mode, access, FileShare.None); } @@ -241,10 +229,8 @@ public static Stream Open( /// A to the file. public static Stream OpenRead(this IFileProvider provider, FilePath path) { - if (provider == null) - { - throw new ArgumentNullException(nameof(provider)); - } + ArgumentNullException.ThrowIfNull(provider); + ArgumentNullException.ThrowIfNull(path); return provider.Retrieve(path).Open(FileMode.Open, FileAccess.Read, FileShare.Read); } @@ -258,11 +244,152 @@ public static Stream OpenRead(this IFileProvider provider, FilePath path) /// A to the file. public static Stream OpenWrite(this IFileProvider provider, FilePath path) { - if (provider == null) - { - throw new ArgumentNullException(nameof(provider)); - } + ArgumentNullException.ThrowIfNull(provider); + ArgumentNullException.ThrowIfNull(path); + + return provider.Retrieve(path) + .Open(FileMode.Create, FileAccess.Write, FileShare.None); + } + + /// + /// Opens a text file, reads all the text in the file, and then closes the file. + /// + /// The file provider. + /// The file to read from. + /// The encoding applied to the contents of the file. + /// A string containing all text in the file. + public static string ReadAllText(this IFileProvider provider, FilePath path, Encoding? encoding = null) + { + ArgumentNullException.ThrowIfNull(provider); + ArgumentNullException.ThrowIfNull(path); + + return provider.Retrieve(path) + .ReadAllText(encoding); + } + + /// + /// Asynchronously opens a text file, reads all the text in the file, and then closes the file. + /// + /// The file provider. + /// The file path. + /// + /// The token to monitor for cancellation requests. + /// The default value is . + /// + /// + /// A task that represents the asynchronous read operation, + /// which wraps the string containing all text in the file. + /// + public static async Task ReadAllTextAsync( + this IFileProvider provider, + FilePath path, + CancellationToken cancellationToken = default) + { + ArgumentNullException.ThrowIfNull(provider); + ArgumentNullException.ThrowIfNull(path); + + return await provider.Retrieve(path) + .ReadAllTextAsync(cancellationToken); + } + + /// + /// Asynchronously opens a text file, reads all the text in the file, and then closes the file. + /// + /// The file provider. + /// The file path. + /// The encoding applied to the contents of the file. + /// + /// The token to monitor for cancellation requests. + /// The default value is . + /// + /// + /// A task that represents the asynchronous read operation, + /// which wraps the string containing all text in the file. + /// + public static async Task ReadAllTextAsync( + this IFileProvider provider, + FilePath path, + Encoding encoding, + CancellationToken cancellationToken = default) + { + ArgumentNullException.ThrowIfNull(provider); + ArgumentNullException.ThrowIfNull(path); + + return await provider.Retrieve(path) + .ReadAllTextAsync(encoding, cancellationToken); + } + + /// + /// Creates a new file, writes the specified string to the file, and then closes the file. + /// If the target file already exists, it is overwritten. + /// + /// The file provider. + /// The file to write to. + /// The string to write to the file. + /// The encoding to apply to the string. + public static void WriteAllText( + this IFileProvider provider, + FilePath path, + string contents, + Encoding? encoding = null) + { + ArgumentNullException.ThrowIfNull(provider); + ArgumentNullException.ThrowIfNull(path); + + provider.Retrieve(path) + .WriteAllText(contents, encoding); + } + + /// + /// Creates a new file, writes the specified string to the file, and then closes the file. + /// If the target file already exists, it is overwritten. + /// + /// The file provider. + /// The file to write to. + /// The string to write to the file. + /// + /// The token to monitor for cancellation requests. + /// The default value is . + /// + /// A task that represents the asynchronous write operation. + public static async Task WriteAllTextAsync( + this IFileProvider provider, + FilePath path, + string contents, + CancellationToken cancellationToken = default) + { + ArgumentNullException.ThrowIfNull(provider); + ArgumentNullException.ThrowIfNull(path); + + await provider.Retrieve(path) + .WriteAllTextAsync(contents, cancellationToken); + } + + /// + /// Asynchronously creates a new file, writes the specified string to the file using the + /// specified encoding, and then closes the file. If the target file already exists, + /// it is truncated and overwritten. + /// + /// The file provider. + /// The file to write to. + /// The string to write to the file. + /// The encoding to apply to the string. + /// + /// The token to monitor for cancellation requests. + /// The default value is . + /// + /// A task that represents the asynchronous write operation. + public static async Task WriteAllTextAsync( + this IFileProvider provider, + FilePath path, + string contents, + Encoding encoding, + CancellationToken cancellationToken = default) + { + ArgumentNullException.ThrowIfNull(provider); + ArgumentNullException.ThrowIfNull(path); - return provider.Retrieve(path).Open(FileMode.Create, FileAccess.Write, FileShare.None); + await provider.Retrieve(path) + .WriteAllTextAsync(contents, encoding, cancellationToken); } } \ No newline at end of file