---
title: "How do you create a new file using FileStream in C#?"  
description: "How do you create a new file using FileStream in C#?"  
author: "ICSM Computer"  
published: 2025-05-05  
updated: 2025-05-05  
canonical: https://www.mindstick.com/interview/34081/how-do-you-create-a-new-file-using-filestream-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# How do you create a new file using FileStream in C#?

To create a new file using `FileStream` in C#, you can use the `FileStream` constructor with the appropriate `FileMode`. Here's a basic example:

### Example: Create a new file using `FileStream`

```cs
using System;
using System.IO;

class Program
{
    static void Main()
    {
        string filePath = "example.txt";

        // Create a new file. If the file already exists, it will be overwritten.
        using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
        {
            byte[] content = System.Text.Encoding.UTF8.GetBytes("Hello, FileStream!");
            fs.Write(content, 0, content.Length);
        }

        Console.WriteLine("File created successfully.");
    }
}
```

### Key Parameters:

1. `FileMode.Create`: Creates a new file or overwrites the existing one.
2. `FileAccess.Write`: Grants write access to the file.
3. `using`: Ensures the file stream is properly closed and disposed.

## Answers

### Answer by ICSM Computer

To create a new file using `FileStream` in C#, you can use the `FileStream` constructor with the appropriate `FileMode`. Here's a basic example:

### Example: Create a new file using `FileStream`

```cs
using System;
using System.IO;

class Program
{
    static void Main()
    {
        string filePath = "example.txt";

        // Create a new file. If the file already exists, it will be overwritten.
        using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
        {
            byte[] content = System.Text.Encoding.UTF8.GetBytes("Hello, FileStream!");
            fs.Write(content, 0, content.Length);
        }

        Console.WriteLine("File created successfully.");
    }
}
```

### Key Parameters:

1. `FileMode.Create`: Creates a new file or overwrites the existing one.
2. `FileAccess.Write`: Grants write access to the file.
3. `using`: Ensures the file stream is properly closed and disposed.


---

Original Source: https://www.mindstick.com/interview/34081/how-do-you-create-a-new-file-using-filestream-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
