Arturito.net

Come to The Dark Side, We Have Cookies!

Reading large text files with C# and Visual Studio 2008

without comments

I managed to read text file with 200 000 lines within 4 seconds.
on Pentium 4 2.4GHZ with 500MB of RAM (Virtual Machine)


// this code assumes the following using statements
// using System.IO;
// using System.Text.RegularExpressions;

StreamReader sr = new StreamReader(openFileDialog1.FileName);
string[] arr = Regex.Split(sr.ReadToEnd(), @"\r\n");
sr.Close();
foreach(string s in arr) {
if (s.Contains("SOMEWORD"))
 {                            
tbLog.AppendText(s.Trim() + Environment.NewLine);
}

}

Source:

http://www.dotnet2themax.com/ShowContent.aspx?ID=4ee44d6c-79a9-466d-ab47-56bba526534f

Written by guru

March 12th, 2010 at 1:27 pm

Posted in C sharp