---
title: "Merge many text files into single text file using C#?"  
description: "Merge many text files into single text file using C#?"  
author: "Lady Bird Johnson"  
published: 2013-10-16  
updated: 2013-10-16  
canonical: https://www.mindstick.com/forum/1684/merge-many-text-files-into-single-text-file-using-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 1 minute  

---

# Merge many text files into single text file using C#?

I want to merge all of the tab-delimited text [files](https://www.mindstick.com/articles/23302/the-importance-and-advantage-of-keeping-your-important-files-on-the-cloud) in a [directory](https://www.mindstick.com/forum/226/how-to-get-application-directory-using-c-sharp-csharp) into one giant [text file](https://www.mindstick.com/forum/12828/converting-a-text-file-to-an-array-in-java). The files don't have headers and the columns in all of the files are all aligned properly with each other so let's assume we don't have to worry about formatting [consistency issues](https://www.mindstick.com/interview/34005/how-can-you-handle-cache-consistency-issues).

I just have to stitch/[join](https://www.mindstick.com/articles/1487/join-sleep-and-interrupt-methods-in-c-sharp-threading)/merge all of the files [together](https://yourviews.mindstick.com/view/80819/live-in-relationship-protecting-the-right-to-live-together) in no particular order.

Here's my [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) that works:

\

```
   string[] stringArray = Directory.GetFiles(@"C:\MergeFileName", "*.txt");
     System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
     for (int i = 0; i <= stringArray.Count(); i++)
    {
        stringBuilder.Append(System.IO.File.ReadAllText(stringArray[i]));
    }
     string bulidOutput = stringBuilder.ToString();
     string newFilePath = @"C:\NewMergeFileName.txt";
    System.IO.File.WriteAllText(newFilePath, bulidOutput);
```

is there any other way which is better/[faster](https://yourviews.mindstick.com/story/1515/5-ways-to-get-in-shape-faster)/more concise doing this?

## Replies

### Reply by Anonymous User

```
using (var bulidOutput = File.Create("bulidOutput"))
{
    foreach (var file in new[] { "firstFile", "secondFile" })
    {
        using (var input = File.OpenRead(file))
        {
            input.CopyTo(bulidOutput);
        }
    }
}
```


---

Original Source: https://www.mindstick.com/forum/1684/merge-many-text-files-into-single-text-file-using-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
