Tuesday, May 13, 2008

Multiple file reading in C#

Today I met one situation that I have to read all text files under one general folder.One of My senior Mr.Jignesh hada developed an utility to convert the raw text into DataSet,then it will be taken out as Crystal Report in C#.
I learned many things from him just sitting and watching carefully.Then he suggest me that he have more than 20 files of text file those has to be merged as one as well as create one Database.
He asked me to merge all notepad files.
Simple I read all textfiles then on the fly i created a text files thats going to be serve as Main file for creating DataSet.

Here is the code snippet to read all text files under one folder.
string []files = Directory.GetFiles(@"D:\Murugesan\");
foreach(string sp in files)

StreamReader sr = new StreamReader(sp);
string contents = sr.ReadToEnd();
//use pre tag with contents as concatenation.