What’s the fastest way to read a text file line-by-line?

I want to read a text file line by line. I wanted to know if I’m doing it as efficiently as possible within the .NET C# scope of things.

This is what I’m trying so far:

var filestream = new System.IO.FileStream(textFilePath,
                                          System.IO.FileMode.Open,
                                          System.IO.FileAccess.Read,
                                          System.IO.FileShare.ReadWrite);
var file = new System.IO.StreamReader(filestream, System.Text.Encoding.UTF8, true, 128);

while ((lineOfText = file.ReadLine()) != null)
{
    //Do something with the lineOfText
}

8 Answers
8

Leave a Comment