How to read a large (1 GB) txt file in .NET?
.net, c#, streamreader
Solution
If you are using .NET 4.0, try MemoryMappedFile which is a designed class for this scenario.
You can use `StreamReader.ReadLine` otherwise.
Problem
I have a 1 GB text file which I need to read line by line. What is the best and fastest way to do this? ``` private void ReadTxtFile() { string filePath = string.Empty; filePath = openFileDialog1.FileName; if (string.IsNullOrEmpty(filePath)) { using (StreamReader sr = new StreamReader(filePath)) { String line; while ((line = sr.ReadLine()) != null) { FormatData(line); } } } } ``` In `FormatData()` I check the starting word of line which must be matched with a word and based on that increment an integer variable. ``` void FormatData(string line) { if (line.StartWith(word)) { globalIntVariable++; } } ```