-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileNodeExtensions.cs
More file actions
103 lines (94 loc) · 5.03 KB
/
FileNodeExtensions.cs
File metadata and controls
103 lines (94 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using System.Text;
namespace Ramstack.FileProviders;
/// <summary>
/// Provides extension methods for the <see cref="FileNode"/>.
/// </summary>
public static class FileNodeExtensions
{
/// <summary>
/// Reads all the text in the file with the specified encoding.
/// </summary>
/// <param name="file">The file from which to read the entire text content.</param>
/// <param name="encoding">The encoding applied to the contents.</param>
/// <returns>
/// A string containing all text in the file.
/// </returns>
public static string ReadAllText(this FileNode file, Encoding? encoding = null) =>
file.ToFileInfo().ReadAllText(encoding);
/// <summary>
/// Reads all lines of the file with the specified encoding.
/// </summary>
/// <param name="file">The file to read from.</param>
/// <param name="encoding">The encoding applied to the contents.</param>
/// <returns>
/// A string array containing all lines of the file.
/// </returns>
public static string[] ReadAllLines(this FileNode file, Encoding? encoding = null) =>
file.ToFileInfo().ReadAllLines(encoding);
/// <summary>
/// Reads the entire contents of the current file into a byte array.
/// </summary>
/// <param name="file">The file to read from.</param>
/// <returns>
/// A byte array containing the contents of the file.
/// </returns>
public static byte[] ReadAllBytes(this FileNode file) =>
file.ToFileInfo().ReadAllBytes();
/// <summary>
/// Asynchronously reads all the text in the current file with the specified encoding.
/// </summary>
/// <param name="file">The file from which to read the entire text content.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask{TResult}"/> representing the asynchronous operation,
/// containing the full text from the current file.
/// </returns>
public static ValueTask<string> ReadAllTextAsync(this FileNode file, CancellationToken cancellationToken = default) =>
file.ToFileInfo().ReadAllTextAsync(cancellationToken);
/// <summary>
/// Asynchronously reads all the text in the current file with the specified encoding.
/// </summary>
/// <param name="file">The file from which to read the entire text content.</param>
/// <param name="encoding">The encoding applied to the contents.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask{TResult}"/> representing the asynchronous operation,
/// containing the full text from the current file.
/// </returns>
public static ValueTask<string> ReadAllTextAsync(this FileNode file, Encoding? encoding, CancellationToken cancellationToken = default) =>
file.ToFileInfo().ReadAllTextAsync(encoding, cancellationToken);
/// <summary>
/// Asynchronously reads all lines of the current file.
/// </summary>
/// <param name="file">The file to read from.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask{TResult}"/> representing the asynchronous operation,
/// containing an array of all lines in the current file.
/// </returns>
public static ValueTask<string[]> ReadAllLinesAsync(this FileNode file, CancellationToken cancellationToken = default) =>
file.ToFileInfo().ReadAllLinesAsync(cancellationToken);
/// <summary>
/// Asynchronously reads all lines of the current file with the specified encoding.
/// </summary>
/// <param name="file">The file to read from.</param>
/// <param name="encoding">The encoding applied to the contents.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask{TResult}"/> representing the asynchronous operation,
/// containing an array of all lines in the current file.
/// </returns>
public static ValueTask<string[]> ReadAllLinesAsync(this FileNode file, Encoding? encoding, CancellationToken cancellationToken = default) =>
file.ToFileInfo().ReadAllLinesAsync(encoding, cancellationToken);
/// <summary>
/// Asynchronously reads the entire contents of the current file into a byte array.
/// </summary>
/// <param name="file">The file to read from.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask{TResult}"/> representing the asynchronous operation,
/// containing an array of the file's bytes.
/// </returns>
public static ValueTask<byte[]> ReadAllBytesAsync(this FileNode file, CancellationToken cancellationToken = default) =>
file.ToFileInfo().ReadAllBytesAsync(cancellationToken);
}