To add @SLaks to the answer, here will demonstrate (using a file with several thousand lines of text):
Stream s = File.OpenRead(path); StreamReader sr = new StreamReader(s, Encoding.ASCII); Console.WriteLine(sr.ReadLine()); s.Dispose(); int i = 1; try { while (!sr.EndOfStream) { Console.WriteLine(sr.ReadLine()); i++; } } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.WriteLine(i + " lines total"); Console.ReadLine();
It will output many and many lines, for example, a couple of hundred, and then throw an exception. My output ended as follows:
qrs tuv wxy zab cde fgh ijk lmn Cannot access a closed file. 204 lines total
In fact, we see that there is a constructor for StreamReader that takes a bufferSize parameter as the fourth parameter:
StreamReader sr = new StreamReader(s, Encoding.ASCII, false, 10000);
Using 10000, it actually prints a total of 1248 lines for me before the crash. Also, the smallest possible value you can use is 1, in which case it still preselects 25 rows.
source share