---
title: "How can you append lines to a file using StreamWriter without overwriting existing content?"  
description: "How can you append lines to a file using StreamWriter without overwriting existing content?"  
author: "Ravi Vishwakarma"  
published: 2025-05-08  
updated: 2025-05-29  
canonical: https://www.mindstick.com/forum/161596/how-can-you-append-lines-to-a-file-using-streamwriter-without-overwriting-existing-content  
category: "c#"  
tags: ["c#", "file handling"]  
reading_time: 1 minute  

---

# How can you append lines to a file using StreamWriter without overwriting existing content?

How can you append lines to a [file](https://www.mindstick.com/articles/59/encrypting-and-decrypting-files-using-c-sharp) using `StreamWriter` without overwriting existing [content](https://www.mindstick.com/articles/13019/get-the-best-content-marketing-pricing-packages-for-your-brand)?

## Replies

### Reply by Ravi Vishwakarma

To **append lines to a file using** `StreamWriter` **in C# without overwriting** existing content, you need to pass `append: true` to the constructor.

### Example: Append Lines Using StreamWriter

```cs
using System.IO;

string filePath = "example.txt";

using (StreamWriter writer = new StreamWriter(filePath, append: true))
{
    writer.WriteLine("This is a new line being appended.");
    writer.WriteLine("Another appended line.");
}
```

### Explanation:

1. `append: true` tells `StreamWriter` to **open the file in append mode** instead of overwriting it.
2. If the file doesn't exist, it will be **created automatically**.


---

Original Source: https://www.mindstick.com/forum/161596/how-can-you-append-lines-to-a-file-using-streamwriter-without-overwriting-existing-content

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
