I have a directory full of hundreds of text files. I need to loop through each of them, writing (appending) their contents to a single file.
This code is overwriting the target file with each new file the StreamReader opens. How do I append the text to the bottom of the target file instead of overwriting it?
static void Main(string args)
var sourceDirectory = @"C:\target\new\";
var txtFiles = Directory.EnumerateFiles(sourceDirectory, "*.txt");
foreach (string currentFile in txtFiles)
StreamReader exportedfiles = new StreamReader(currentFile, System.Text.Encoding.Default);
using (FileStream readStream = File.OpenRead(currentFile))
//Specify path for the combined file that will be created.
using (FileStream writeStream = File.OpenWrite("C:\\file.txt"))
BinaryReader reader = new BinaryReader(readStream);
BinaryWriter writer = new BinaryWriter(writeStream);
byte buffer = new byte; //Equals a 10mb buffer.
while ((bytesRead = readStream.Read(buffer, 0, 10240)) > 0)
writeStream.Write(buffer, 0, bytesRead);