FileHelper
FileHelper
class provides synchronous and asynchronous methods for the write, read and delete a single file.
SaveFile()
Synchronously saves a byte array to a file by an absolute path.
By default, if the file exists, the FileHelper
creates a new file with a unique name and returns that name.
All parameters:
Param name | Param type | Description |
---|---|---|
path |
| The absolute file path to save to. |
bytes |
| The bytes to save to the file. |
createDirIfNotExists |
| The flag indicates to create destination directory if not exists. Default value is |
useSequentialFileNameIfExists |
| The flag indicates that target file name should be changed by adding sequential postfix if file with specfied name already exists. Default value is |
SaveFileAsync()
Asynchronously saves a byte array to a file by an absolute path.
By default, if the file exists, the FileHelper
creates a new file with a unique name and returns that name.
All parameters:
Param name | Param type | Description |
---|---|---|
path |
| The absolute file path to save to. |
bytes |
| The bytes to save to the file. |
createDirIfNotExists |
| The flag indicates to create destination directory if not exists. Default value is |
useSequentialFileNameIfExists |
| The flag indicates that target file name should be changed by adding sequential postfix if file with specfied name already exists. Default value is |
cancellationToken |
| The token to monitor for cancellation requests. Default value is |
ReadFile()
Synchronously reads a file from an absolute path to a byte array.
All parameters:
Param name | Param type | Description |
---|---|---|
path |
| The absolute file path to read to. |
throwIfNotExists |
| The flag indicates to throw exception if file not exists. Default value is |
You can also synchronously reads a file from a stream to a byte array.
All parameters:
Param name | Param type | Description |
---|---|---|
stream |
| The Stream (MemoryStream, FileStream, etc.). |
initialLength |
| Length of returning byte array. Default value is |
ReadFileAsync()
Asynchronously reads a file from an absolute path to a byte array.
All parameters:
Param name | Param type | Description |
---|---|---|
path |
| The absolute file path to read to. |
throwIfNotExists |
| The flag indicates to throw exception if file not exists. Default value is |
cancellationToken |
| The token to monitor for cancellation requests. Default value is |
You can also asynchronously reads a file from a stream to a byte array.
All parameters:
Param name | Param type | Description |
---|---|---|
stream |
| The Stream (MemoryStream, FileStream, etc.). |
initialLength |
| Length of returning byte array. Default value is |
cancellationToken |
| The token to monitor for cancellation requests. Default value is |
DeleteFile()
Deletes a file if it exists.
All parameters:
Param name | Param type | Description |
---|---|---|
path |
| The absolute file path to delete to. |
GetFilePathWithSequentialFileName()
Returns an absolute path with variable destination file name with sequential postfix e.g. ("file_1.txt", "file_2.txt").
All parameters:
Param name | Param type | Description |
---|---|---|
path |
| The absolute file path. |
Last updated