concatenate 5 files dropping blank lines, using c# dot net

Using Visual Studio 2013 c# project.

I want to read 5 text files into a single file skipping over blank lines in the 5 input files.

What's the best way of doing this.

I would like to use streamWriter but I can't see a way to append.

But any other way is fine with me as long as it works.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

To append use this.
new StreamWriter("c:\\file.txt", true);

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gateguardAuthor Commented:
So this writes to a new file:

new StreamWriter("c:\\file.txt");

And this appends to an existing file:

new StreamWriter("c:\\file.txt", true);


JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

Franck GaspozSoftware Architect, Technical ExpertCommented:
A solution using both StreamReader and StreamWriter could look like :

using System.IO;

// a method to get file lines
public List<string> GetFileLines(string Path)
            List<string> Res = new List<string>();
            using (StreamReader sr = new StreamReader(Path))
                while (!sr.EndOfStream)
            return Res;

// a method to write lines into a file (create or replace existing file)
public void WriteFile(string Path, List<string> Lines)
            using (StreamWriter sw = new StreamWriter(Path))
                foreach ( String S in Lines )

// a method to Append lines into a file
public void AppendLines(string Path, List<string> Lines)
            using (StreamWriter sw = File.AppendText(Path) )
                foreach ( String S in Lines )

and now let's do the stuff :

List<string> F1 = GetFileLines("path to file 1");
List<string> F2 = GetFileLines("path to file 2");
List<string> F3 = GetFileLines("path to file 3");
List<string> F3 = GetFileLines("path to file 4");
List<string> F5 = GetFileLines("path to file 5");
string OutputFile = "path to output file";
WriteFile(OutputFile,F1.Where(x => !string.IsNullOrWhiteSpace(x)).ToList());
AppendLines(OutputFile,F2.Where(x => !string.IsNullOrWhiteSpace(x)).ToList());
AppendLines(OutputFile,F3.Where(x => !string.IsNullOrWhiteSpace(x)).ToList());
AppendLines(OutputFile,F4.Where(x => !string.IsNullOrWhiteSpace(x)).ToList());
AppendLines(OutputFile,F5.Where(x => !string.IsNullOrWhiteSpace(x)).ToList());

non blank lines are append to the output file (created or replaced), in the order of the 5 files
gateguardAuthor Commented:
Thanks to both of you.
Fernando SotoRetiredCommented:
Hi gateguard;

A little late but have a look at this.

using System.IO;
using System.Text.RegularExpressions;

// Get the file names that are going to be appended
DirectoryInfo dir = new DirectoryInfo("C:/Working Directory/");
List<string> fnames = dir.GetFiles("file?.txt").Select(f => f.FullName).ToList();

// String to hold appended text
String fileText = "";

// Read and append data
foreach(string fn in fnames)
    string ft = File.ReadAllText(fn).Trim() + "\r\n";
    fileText += ft;

// Remove all blank line
fileText = Regex.Replace(fileText, "(\r\n){2,}", "\r\n");

// Create a StreamWriter
StreamWriter sw = new StreamWriter("C:/Working Directory/filesAppended.txt");
// Write the data out
// Close the file.

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.