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.

FileHelper.SaveFile("absolute_file_path", bytes);

By default, if the file exists, the FileHelper creates a new file with a unique name and returns that name.

string existingFilePath = "D:\\readme.txt";
string newFilePath = FileHelper.SaveFile(existingFilePath, bytes); // Returns "D:\\readme_1.txt".

All parameters:

SaveFileAsync()

Asynchronously saves a byte array to a file by an absolute path.

await FileHelper.SaveFileAsync("absolute_file_path", bytes);

By default, if the file exists, the FileHelper creates a new file with a unique name and returns that name.

string existingFilePath = "D:\\readme.txt";
string newFilePath = await FileHelper.SaveFileAsync(existingFilePath, bytes); // Returns "D:\\readme_1.txt".

All parameters:

ReadFile()

Synchronously reads a file from an absolute path to a byte array.

byte[] bytes = FileHelper.ReadFile(filePath);

All parameters:

You can also synchronously reads a file from a stream to a byte array.

byte[] bytes = FileHelper.ReadFile(stream);

All parameters:

ReadFileAsync()

Asynchronously reads a file from an absolute path to a byte array.

byte[] bytes = await FileHelper.ReadFileAsync(filePath);

All parameters:

You can also asynchronously reads a file from a stream to a byte array.

byte[] bytes = await FileHelper.ReadFileAsync(stream);

All parameters:

DeleteFile()

Deletes a file if it exists.

FileHelper.DeleteFile(filePath);

All parameters:

GetFilePathWithSequentialFileName()

Returns an absolute path with variable destination file name with sequential postfix e.g. ("file_1.txt", "file_2.txt").

string existingFilePath = "D:\\readme.txt";
string newFilePath = FileHelper.GetFilePathWithSequentialFileName(existingFilePath); // Returns "D:\\readme_1.txt".

All parameters:

Last updated