Skip to content

Instantly share code, notes, and snippets.

@csharpforevermore
Created March 11, 2019 16:49
Show Gist options
  • Save csharpforevermore/789403da49b05a53718ee12c2491a3ad to your computer and use it in GitHub Desktop.
Save csharpforevermore/789403da49b05a53718ee12c2491a3ad to your computer and use it in GitHub Desktop.
Counts the number of lines in a text file
// 4.0
var lineCount = File.ReadLines(@"C:\file.txt").Count();
// pre-4.0
var lineCount = File.ReadAllLines(@"C:\file.txt").Length;
// pre-4.0 (more efficient)
var lineCount = 0;
using (var reader = File.OpenText(@"C:\file.txt"))
{
while (reader.ReadLine() != null)
{
lineCount++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment