A better way would be to use something like:
var count = File.ReadLines("file.txt").Count();
This will only work in .NET 4, but will read one line at a time. If you are happy enough to load the entire file into memory in one go, you can use:
var count = File.ReadAllLines("file.txt").Length;
Please note that if the file is large (or it is on a network drive, etc.), this can take a lot of time, in which case you will want to do this from the user interface stream.
source
share