First step is selecting the file paths and then read the file using Stream Reader and save the content to the string builder. After doing this for all the files, now our content is in the string builder.
Now create a file with the file stream (using file stream is easily can easily kill the process). Now write the content of the string builder to the newly created file using the Stream Writer.
I have done this in winforms. You can download the demo here.
Our UI looks like this.
UI consists of two buttons, one(btnBrowse) for browsing or selecting the text files and another(btnSaveResult) for saving the new file with the combined content. One textbox for viewing the combined content. Apart from these controls we have added one OpenFileDialog control and one SaveFileDialog control for selecting the files and saving the newly created file respectively.
Now lets jump into the code.
public partial class Form1 : Form
private void btnBrowse_Click(object sender, EventArgs e)
private void ofdTextFile_FileOk(object sender, CancelEventArgs e)
List<string> myfiles = new List<string>();
StringBuilder sb = new StringBuilder(“hi”);
foreach (string path in ofdTextFile.FileNames)
string filepath = path;
StreamReader myfile = new StreamReader(filepath);
//txtEditor.Text = filepath;
txtEditor.Text = sb.ToString();
private void btnSaveResult_Click(object sender, EventArgs e)
private void saveFileDialog_FileOk(object sender, CancelEventArgs e)
using (fs = File.Create(saveFileDialog.FileName.ToString()))
using (StreamWriter sw = new StreamWriter(saveFileDialog.FileName.ToString()))
- Opening the file dialog on the btnBrowse Click.
- On file selection completed we are getting the selected file paths, reading the content of each file by appending it to the string builder and finally displaying the string builder content to the text box (txtEditor).
- Opening the save dialog on the btnSaveResult Click.
- On file saving we are creating a file with name specified in the save file dialog and after creating the file, writing the content to the newly created file.