# 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.

```csharp
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.

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

All parameters:

| Param name                    | Param type | Description                                                                                                                                                  |
| ----------------------------- | ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| path                          | `string`   | The absolute file path to save to.                                                                                                                           |
| bytes                         | `byte[]`   | The bytes to save to the file.                                                                                                                               |
| createDirIfNotExists          | `bool`     | The flag indicates to create destination directory if not exists. Default value is `true`.                                                                   |
| useSequentialFileNameIfExists | `bool`     | The flag indicates that target file name should be changed by adding sequential postfix if file with specfied name already exists. Default value is `false`. |

## SaveFileAsync()

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

```csharp
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.

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

All parameters:

| Param name                    | Param type          | Description                                                                                                                                                  |
| ----------------------------- | ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| path                          | `string`            | The absolute file path to save to.                                                                                                                           |
| bytes                         | `byte[]`            | The bytes to save to the file.                                                                                                                               |
| createDirIfNotExists          | `bool`              | The flag indicates to create destination directory if not exists. Default value is `true`.                                                                   |
| useSequentialFileNameIfExists | `bool`              | The flag indicates that target file name should be changed by adding sequential postfix if file with specfied name already exists. Default value is `false`. |
| cancellationToken             | `CancellationToken` | The token to monitor for cancellation requests. Default value is `None`.                                                                                     |

## ReadFile()

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

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

All parameters:

| Param name       | Param type | Description                                                                        |
| ---------------- | ---------- | ---------------------------------------------------------------------------------- |
| path             | `string`   | The absolute file path to read to.                                                 |
| throwIfNotExists | `bool`     | The flag indicates to throw exception if file not exists. Default value is `true`. |

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

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

All parameters:

| Param name    | Param type | Description                                           |
| ------------- | ---------- | ----------------------------------------------------- |
| stream        | `Stream`   | The Stream (MemoryStream, FileStream, etc.).          |
| initialLength | `long`     | Length of returning byte array. Default value is `0`. |

## ReadFileAsync()

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

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

All parameters:

| Param name        | Param type          | Description                                                                        |
| ----------------- | ------------------- | ---------------------------------------------------------------------------------- |
| path              | `string`            | The absolute file path to read to.                                                 |
| throwIfNotExists  | `bool`              | The flag indicates to throw exception if file not exists. Default value is `true`. |
| cancellationToken | `CancellationToken` | The token to monitor for cancellation requests. Default value is `None`.           |

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

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

All parameters:

| Param name        | Param type          | Description                                                              |
| ----------------- | ------------------- | ------------------------------------------------------------------------ |
| stream            | `Stream`            | The Stream (MemoryStream, FileStream, etc.).                             |
| initialLength     | `long`              | Length of returning byte array. Default value is `0`.                    |
| cancellationToken | `CancellationToken` | The token to monitor for cancellation requests. Default value is `None`. |

## DeleteFile()

Deletes a file if it exists.

```csharp
FileHelper.DeleteFile(filePath);
```

All parameters:

| Param name | Param type | Description                          |
| ---------- | ---------- | ------------------------------------ |
| path       | `string`   | 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").

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

All parameters:

| Param name | Param type | Description             |
| ---------- | ---------- | ----------------------- |
| path       | `string`   | The absolute file path. |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.structr.dev/utilities/io/filehelper.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
