Take all text files in a folder and combine then into 1

0

I'm trying to merge all my text files into one file. The problem I am having is that the file names are based on data previously captured in my app. I don't know how to define my path to where the text files are, maybe. I keep getting a error, but the path to the files are correct. What am I missing?

string filesread = System.AppDomain.CurrentDomain.BaseDirectory + @"\data\Customers\" + CustComboB.SelectedItem + @"\";
        Directory.GetFiles(filesread);
        using (var output = File.Create("allfiles.txt"))
        {
            foreach (var file in new[] { filesread })
            {
                using (var input = File.OpenRead(file))
                {
                    input.CopyTo(output);
                }
            }
        }
        System.Diagnostics.Process.Start("allfiles.txt");

my error:

System.IO.DirectoryNotFoundException

HResult=0x80070003 Message=Could not find a part of the path 'C:\Users\simeo\source\repos\UpMarker\UpMarker\bin\Debug\data\Customers\13Dec2018\'.

I cant post a pic, but let me try and give some more details on my form. I select a combobox item, this item is a directory. then I have a listbox that displays the files in my directory. I then have a button that executes my desires of combining the files. thanks

merge
combobox
listbox
text-files
path-combine
asked on Stack Overflow Dec 16, 2018 by user2768342

1 Answer

0

I finally got it working.

string path = @"data\Customers\" + CustComboB.SelectedItem;
        string topath = @"data\Customers\";
        string files = "*.txt";
        string[] txtFiles;
        txtFiles = Directory.GetFiles(path, files);
        using (StreamWriter writer = new StreamWriter(topath + @"\allfiles.txt"))
        {
            for (int i = 0; i < txtFiles.Length; i++)
            {
                using (StreamReader reader = File.OpenText(txtFiles[i]))
                {
                    writer.Write(reader.ReadToEnd());

                }
            }
            System.Diagnostics.Process.Start(topath + @"\allfiles.txt");
        }
answered on Stack Overflow Dec 16, 2018 by user2768342

User contributions licensed under CC BY-SA 3.0