TextFileUtility Class

Provides methods to read a text file together with its encoding, and to write a text file only when its text changes.

public static class TextFileUtility

Methods

NameDescription
ReadAllText(string, Encoding?)Reads the text of a file, together with the encoding to write the file back with.
ReadAllTextAsync(string, Encoding?, CancellationToken)Asynchronously reads the text of a file, together with the encoding to write the file back with.
WriteAllTextIfChanged(string, string, Encoding?)Writes a text to a file, unless the file holds that text already.
WriteAllTextIfChangedAsync(string, string, Encoding?, CancellationToken)Asynchronously writes a text to a file, unless the file holds that text already.

ReadAllText(string, Encoding?)

Reads the text of a file, together with the encoding to write the file back with.

public static TextFileContent ReadAllText(string path, Encoding? fallbackEncoding = null)

Parameters

path string
The path of the file to read.
fallbackEncoding Encoding?
The encoding of a file that has no byte order mark, or null for UTF-8 without a byte order mark. A fallback encoding that emits a byte order mark is returned as it is, so a file written back with it gets a byte order mark.

Returns

TextFileContent The text of the file, and the encoding to write the file back with.

Exceptions

ArgumentNullException
path is null.
FileNotFoundException
The file does not exist.

Remarks

This method recognizes the byte order marks of UTF-8, UTF-16, and UTF-32. When the file starts with one, the returned encoding is the encoding of that byte order mark, and emits it. When the file starts with none, the returned encoding is the fallback encoding. A file written back with the returned encoding keeps the byte order mark it had, and a file that had none gets none, unless the fallback encoding emits one.

A file that starts with the byte order mark of the fallback encoding is read with the fallback encoding, which is also the returned encoding. The encoding of any other byte order mark replaces every invalid byte sequence with the replacement character, U+FFFD. The default fallback encoding does the same, as File.ReadAllText does. To get a DecoderFallbackException for an invalid byte sequence in a file without a byte order mark, pass new UTF8Encoding(false, true) as fallbackEncoding.

For a file it cannot read, this method throws what File.ReadAllText throws.

See also

ReadAllTextAsync(string, Encoding?, CancellationToken)

Asynchronously reads the text of a file, together with the encoding to write the file back with.

public static Task<TextFileContent> ReadAllTextAsync(string path, Encoding? fallbackEncoding = null, CancellationToken cancellationToken = default)

Parameters

path string
The path of the file to read.
fallbackEncoding Encoding?
The encoding of a file that has no byte order mark, or null for UTF-8 without a byte order mark. A fallback encoding that emits a byte order mark is returned as it is, so a file written back with it gets a byte order mark.
cancellationToken CancellationToken
A CancellationToken that, when signaled, ends the read.

Returns

Task<TextFileContent> A Task<TResult> whose result holds the text of the file, and the encoding to write the file back with.

Exceptions

ArgumentNullException
path is null.
FileNotFoundException
The file does not exist.
OperationCanceledException
cancellationToken was signaled before the read ended.

Remarks

This method recognizes the byte order marks of UTF-8, UTF-16, and UTF-32. When the file starts with one, the returned encoding is the encoding of that byte order mark, and emits it. When the file starts with none, the returned encoding is the fallback encoding. A file written back with the returned encoding keeps the byte order mark it had, and a file that had none gets none, unless the fallback encoding emits one.

A file that starts with the byte order mark of the fallback encoding is read with the fallback encoding, which is also the returned encoding. The encoding of any other byte order mark replaces every invalid byte sequence with the replacement character, U+FFFD. The default fallback encoding does the same, as File.ReadAllText does. To get a DecoderFallbackException for an invalid byte sequence in a file without a byte order mark, pass new UTF8Encoding(false, true) as fallbackEncoding.

For a file it cannot read, this method throws what File.ReadAllText throws.

See also

WriteAllTextIfChanged(string, string, Encoding?)

Writes a text to a file, unless the file holds that text already.

public static bool WriteAllTextIfChanged(string path, string text, Encoding? encoding = null)

Parameters

path string
The path of the file to write.
text string
The text to write.
encoding Encoding?
The encoding to read the file with, and to write it with, or null for UTF-8 without a byte order mark.

Returns

bool true if this method wrote the file; false if the file held text already.

Exceptions

ArgumentNullException
path is null. - or - text is null.
DirectoryNotFoundException
The directory of the file does not exist.

Remarks

This method writes the file when the file does not exist, and when its text differs from text by ordinal comparison. A file this method does not write keeps its timestamp.

The comparison is on text, not on bytes. This method reads the file with the encoding it writes with, and detects no encoding from a byte order mark. A file without a byte order mark, compared through an encoding that emits one, counts as unchanged when the text is the same. A file with a byte order mark, compared through an encoding that emits none, counts as changed.

For a file it cannot read or write, this method throws what File.ReadAllText and File.WriteAllText throw.

See also

WriteAllTextIfChangedAsync(string, string, Encoding?, CancellationToken)

Asynchronously writes a text to a file, unless the file holds that text already.

public static Task<bool> WriteAllTextIfChangedAsync(string path, string text, Encoding? encoding = null, CancellationToken cancellationToken = default)

Parameters

path string
The path of the file to write.
text string
The text to write.
encoding Encoding?
The encoding to read the file with, and to write it with, or null for UTF-8 without a byte order mark.
cancellationToken CancellationToken
A CancellationToken that, when signaled, ends the operation.

Returns

Task<bool> A Task<TResult> whose result is true if this method wrote the file; false if the file held text already.

Exceptions

ArgumentNullException
path is null. - or - text is null.
DirectoryNotFoundException
The directory of the file does not exist.
OperationCanceledException
cancellationToken was signaled before the operation ended.

Remarks

This method writes the file when the file does not exist, and when its text differs from text by ordinal comparison. A file this method does not write keeps its timestamp.

The comparison is on text, not on bytes. This method reads the file with the encoding it writes with, and detects no encoding from a byte order mark. A file without a byte order mark, compared through an encoding that emits one, counts as unchanged when the text is the same. A file with a byte order mark, compared through an encoding that emits none, counts as changed.

A cancellationToken signaled while this method writes the file leaves the file incomplete.

For a file it cannot read or write, this method throws what File.ReadAllText and File.WriteAllText throw.

See also