Created
March 11, 2019 16:49
-
-
Save csharpforevermore/789403da49b05a53718ee12c2491a3ad to your computer and use it in GitHub Desktop.
Counts the number of lines in a text file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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
Taken from https://stackoverflow.com/questions/119559/determine-the-number-of-lines-within-a-text-file