---
title: "How do you merge multiple text files into a single file?"  
description: "How do you merge multiple text files into a single file?"  
author: "ICSM Computer"  
published: 2025-05-16  
updated: 2025-05-22  
canonical: https://www.mindstick.com/forum/161633/how-do-you-merge-multiple-text-files-into-a-single-file  
category: "c#"  
tags: ["c#", "file handling"]  
reading_time: 2 minutes  

---

# How do you merge multiple text files into a single file?

How do you [merge](https://www.mindstick.com/startup/15/how-growthpal-helps-companies-make-the-right) [multiple](https://www.mindstick.com/blog/12797/iowa-is-expected-to-see-heavy-growth-in-multiple-sectors) [text](https://www.mindstick.com/blog/301635/did-people-reinvent-texting-to-express-the-full-range-of-emotions) [files](https://www.mindstick.com/articles/23302/the-importance-and-advantage-of-keeping-your-important-files-on-the-cloud) into a single [file](https://www.mindstick.com/articles/59/encrypting-and-decrypting-files-using-c-sharp)?

## Replies

### Reply by Anubhav Sharma

To **merge multiple text files into a single file** in C#, you can use `System.IO` to read each file and append its content to the target file. Here's how:

## Basic Example: Merge All `.txt` Files in a Folder

```cs
using System.IO;

string sourceDirectory = @"C:\TextFiles";
string outputFile = @"C:\Merged\combined.txt";

// Get all .txt files in the folder
var files = Directory.GetFiles(sourceDirectory, "*.txt");

// Merge into one file
using (var output = File.CreateText(outputFile))
{
    foreach (var file in files)
    {
        string content = File.ReadAllText(file);
        output.WriteLine(content);     // Write file content
        output.WriteLine();            // Optional: blank line between files
    }
}
```

## Variation: Append Instead of Overwriting

If you want to **append** to an existing file:

```cs
foreach (var file in files)
{
    File.AppendAllText(outputFile, File.ReadAllText(file) + Environment.NewLine);
}
```

## Optional Enhancements

### Add File Headers

```cs
foreach (var file in files)
{
    var fileName = Path.GetFileName(file);
    File.AppendAllText(outputFile, $"--- {fileName} ---{Environment.NewLine}");
    File.AppendAllText(outputFile, File.ReadAllText(file) + Environment.NewLine);
}
```

### Sort Files by Name or Date

```cs
var sortedFiles = Directory.GetFiles(sourceDirectory, "*.txt")
    .Select(f => new FileInfo(f))
    .OrderBy(f => f.Name) // or .CreationTime
    .Select(f => f.FullName);
```

## Summary

| Operation | Code/Method |
| --- | --- |
| Read content | `File.ReadAllText(path)` |
| Write or create file | `File.CreateText(path)` |
| Append to file | `File.AppendAllText(path, ...)` |
| Merge with headers | Add file name before writing |
| Handle large files | Stream line by line with `StreamReader/Writer` |


---

Original Source: https://www.mindstick.com/forum/161633/how-do-you-merge-multiple-text-files-into-a-single-file

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
