Class FileUtils
Provides file helpers: moves, atomic writes, and reads and writes that wait out another process's sharing.
public static class FileUtils
- Inheritance
-
FileUtils
- Inherited Members
Methods
MoveOrDiscard(string, string)
Moves a file or a folder to a new place, unless the new place has one already, which is then the one in use, so the old one is deleted.
public static bool MoveOrDiscard(string sourcePath, string targetPath)
Parameters
Returns
- bool
true if it was moved or deleted, or the two paths are the same place; false if it is gone, as another process may have moved it first.
Exceptions
- IOException
The move or the deletion failed, as when the new place's folder is missing.
- UnauthorizedAccessException
The process may not move or delete it.
ReadAllTextAsync(string, TimeSpan, CancellationToken)
Reads a file's text, sharing it with readers, and again after 100 milliseconds when it fails with an I/O error, while the time since the first try is under the timeout.
public static Task<string> ReadAllTextAsync(string filePath, TimeSpan timeout, CancellationToken cancellationToken)
Parameters
filePathstringThe file's path.
timeoutTimeSpanHow long to keep trying.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
Exceptions
- IOException
The read still fails at the timeout, or the file is missing then.
- UnauthorizedAccessException
The process may not read the file.
- OperationCanceledException
cancellationTokenwas canceled.
ReplaceInvalidFileNameChars(string)
Replaces each character a file name may not hold with _, such as a path separator; it leaves .. and
reserved names as they are.
public static string ReplaceInvalidFileNameChars(string name)
Parameters
namestringThe name.
Returns
- string
The name with those characters replaced.
TryReadAllTextAsync(string, CancellationToken)
Reads a file's text, or gives null: a missing file, as on a first run, goes unlogged, and any other failure, a file gone after the check included, is logged as a warning.
public static Task<string?> TryReadAllTextAsync(string filePath, CancellationToken cancellationToken)
Parameters
filePathstringThe file's path.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
Exceptions
- OperationCanceledException
cancellationTokenwas canceled; taken for a missing file, it would have the caller make the file anew.
WaitOutSharingAsync(Func<Task>, TimeSpan, CancellationToken)
Runs a file access, and again after 50 milliseconds when it is refused, or fails with an I/O error other than a missing file, while the time since the first try is under the timeout. The token cancels the waits, not the access.
public static Task WaitOutSharingAsync(Func<Task> access, TimeSpan timeout, CancellationToken cancellationToken)
Parameters
accessFunc<Task>The access.
timeoutTimeSpanHow long to keep trying.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task
A task that represents the asynchronous operation.
Exceptions
- IOException
The access still meets the sharing at the timeout, or fails otherwise.
- UnauthorizedAccessException
The access is refused, at the timeout.
- OperationCanceledException
cancellationTokenwas canceled.
WaitOutSharingAsync<T>(Func<Task<T>>, TimeSpan, CancellationToken)
Runs a file access, and again after 50 milliseconds when it is refused, or fails with an I/O error other than a missing file, while the time since the first try is under the timeout; a try under way is not cut, so the whole may last longer. The token cancels the waits between tries, not the access.
public static Task<T> WaitOutSharingAsync<T>(Func<Task<T>> access, TimeSpan timeout, CancellationToken cancellationToken)
Parameters
accessFunc<Task<T>>The access.
timeoutTimeSpanHow long to keep trying.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task<T>
A task whose result is the access's.
Type Parameters
TThe type of the access's result.
Exceptions
- IOException
The access still meets the sharing at the timeout, or fails otherwise.
- UnauthorizedAccessException
The access is refused, at the timeout.
- OperationCanceledException
cancellationTokenwas canceled.
WriteAllBytesAtomicAsync(string, byte[], CancellationToken)
Replaces a file's bytes through a file beside it, as WriteAllTextAtomicAsync(string, string, CancellationToken) does.
public static Task WriteAllBytesAtomicAsync(string filePath, byte[] bytes, CancellationToken cancellationToken)
Parameters
filePathstringThe file's path.
bytesbyte[]The bytes.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task
A task that represents the asynchronous operation.
Exceptions
- IOException
The write or the move failed.
- UnauthorizedAccessException
The process may not write the file.
- OperationCanceledException
cancellationTokenwas canceled.
WriteAllTextAtomic(string, string)
Replaces a file's text through a file beside it, synchronously, for a write behind a setter: a write that fails, as on a full disk, leaves the old file.
public static void WriteAllTextAtomic(string filePath, string content)
Parameters
Exceptions
- IOException
The write or the move failed.
- UnauthorizedAccessException
The process may not write the file.
WriteAllTextAtomicAsync(string, string, CancellationToken)
Replaces a file's text, in UTF-8: the text goes to a file beside it, which is then moved over the file, so a write that fails or is canceled leaves the old file, not a part of the new one. The move is not canceled, and nothing is flushed to the disk first.
public static Task WriteAllTextAtomicAsync(string filePath, string content, CancellationToken cancellationToken)
Parameters
filePathstringThe file's path.
contentstringThe text.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task
A task that represents the asynchronous operation.
Exceptions
- IOException
The write or the move failed.
- UnauthorizedAccessException
The process may not write the file.
- OperationCanceledException
cancellationTokenwas canceled.
WriteAllTextRetryAsync(string, string, TimeSpan, CancellationToken)
Writes a file's text, and again after 100 milliseconds when it fails with an I/O error, while the time since the first try is under the timeout.
public static Task WriteAllTextRetryAsync(string filePath, string content, TimeSpan timeout, CancellationToken cancellationToken)
Parameters
filePathstringThe file's path.
contentstringThe text.
timeoutTimeSpanHow long to keep trying.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task
A task that represents the asynchronous operation.
Exceptions
- IOException
The write still fails at the timeout.
- UnauthorizedAccessException
The process may not write the file.
- OperationCanceledException
cancellationTokenwas canceled.