Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to merge multiple files into one?

I wrote a code that should read all the files in the directory and save to one. Well, for some reason it only reads one file.

 foreach (var file in Directory.GetFiles(directory, "*.*"))
        {
            foreach (var line in File.ReadAllText(file))
            {
                File.WriteAllText(@"D:\ee.txt", line.ToString());
            }
        }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

A simple way of finding the files in a Directory and merging the content into 1 new file is explained here.

string contents = string.Empty;
string[] arr = Directory.GetFiles(@"C:\Jay", "*.txt");
foreach (string file in arr)
{
   contents = File.ReadAllText(file);
   File.AppendAllText(@"C:\Jay\mergedfile.txt", contents, Encoding.Default);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading