---
title: "What is the difference between File.WriteAllText() and File.AppendAllText()?"  
description: "What is the difference between File.WriteAllText() and File.AppendAllText()?"  
author: "ICSM Computer"  
published: 2025-05-05  
updated: 2025-05-19  
canonical: https://www.mindstick.com/forum/161572/what-is-the-difference-between-file-writealltext-and-file-appendalltext  
category: "c#"  
tags: ["c#"]  
reading_time: 1 minute  

---

# What is the difference between File.WriteAllText() and File.AppendAllText()?

What is the [difference](https://www.mindstick.com/articles/157114/good-news-or-bad-news-and-the-difference-is) between `File.WriteAllText()` and `File.AppendAllText()`?

## Replies

### Reply by Anubhav Sharma

The difference between `File.WriteAllText()` and `File.AppendAllText()` in C# is in **how they handle existing file content**:

### `File.WriteAllText(path, contents)`

1. **Overwrites** the file if it already exists.
2. **Creates** a new file if it doesn't exist.
3. Use this when you want to **replace** the file’s content entirely.

## Example:

```cs
File.WriteAllText("example.txt", "Hello, world!");
```

If `example.txt` already has data, it will be replaced with `"Hello, world!"`.

### `File.AppendAllText(path, contents)`

1. **Appends** the text to the end of the file if it exists.
2. **Creates** a new file if it doesn’t exist.
3. Use this when you want to **add content** without deleting existing content.

## Example:

```cs
File.AppendAllText("example.txt", "Appended line\n");
```

If `example.txt` already has `"Hello, world!"`, after this call it will contain:

```plaintext
Hello, world!Appended line
```

### Summary

| Method | Overwrites File | Appends to File | Creates File if Missing |
| --- | --- | --- | --- |
| `File.WriteAllText` | Yes | No | Yes |
| `File.AppendAllText` | No | Yes | Yes |


---

Original Source: https://www.mindstick.com/forum/161572/what-is-the-difference-between-file-writealltext-and-file-appendalltext

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
