---
title: "What are the different FileMode options available in FileStream?"  
description: "What are the different FileMode options available in FileStream?"  
author: "ICSM Computer"  
published: 2025-05-05  
updated: 2025-05-05  
canonical: https://www.mindstick.com/interview/34082/what-are-the-different-filemode-options-available-in-filestream  
category: "c#"  
tags: ["c#", "file handling"]  
reading_time: 3 minutes  

---

# What are the different FileMode options available in FileStream?

The `FileMode` enumeration in C# defines how the `FileStream` should open or create a file. Here's a breakdown of the available options:

### `FileMode` Options

| FileMode | Description |
| --- | --- |
| `Append` | Opens the file if it exists and seeks to the end, or creates a new file. Only for writing. |
| `Create` | Creates a new file. If it exists, **overwrites** it. |
| `CreateNew` | Creates a new file. If the file **already exists**, throws an `IOException`. |
| `Open` | Opens an existing file. If the file **does not exist**, throws a `FileNotFoundException`. |
| `OpenOrCreate` | Opens the file if it exists; otherwise, **creates a new file**. |
| `Truncate` | Opens an existing file and **truncates** it to zero bytes. File **must exist**. |

### Example Usage

```cs
// Create a new file or overwrite if it exists
var fs = new FileStream("file.txt", FileMode.Create);

// Open existing file or throw if not found
var fs2 = new FileStream("file.txt", FileMode.Open);

// Create new file only if it doesn't exist
var fs3 = new FileStream("file.txt", FileMode.CreateNew);
```

Each `FileMode` can be paired with `FileAccess` and `FileShare` for more control.

## Answers

### Answer by ICSM Computer

The `FileMode` enumeration in C# defines how the `FileStream` should open or create a file. Here's a breakdown of the available options:

### `FileMode` Options

| FileMode | Description |
| --- | --- |
| `Append` | Opens the file if it exists and seeks to the end, or creates a new file. Only for writing. |
| `Create` | Creates a new file. If it exists, **overwrites** it. |
| `CreateNew` | Creates a new file. If the file **already exists**, throws an `IOException`. |
| `Open` | Opens an existing file. If the file **does not exist**, throws a `FileNotFoundException`. |
| `OpenOrCreate` | Opens the file if it exists; otherwise, **creates a new file**. |
| `Truncate` | Opens an existing file and **truncates** it to zero bytes. File **must exist**. |

### Example Usage

```cs
// Create a new file or overwrite if it exists
var fs = new FileStream("file.txt", FileMode.Create);

// Open existing file or throw if not found
var fs2 = new FileStream("file.txt", FileMode.Open);

// Create new file only if it doesn't exist
var fs3 = new FileStream("file.txt", FileMode.CreateNew);
```

Each `FileMode` can be paired with `FileAccess` and `FileShare` for more control.


---

Original Source: https://www.mindstick.com/interview/34082/what-are-the-different-filemode-options-available-in-filestream

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
