I want to know how many lines I have in my file. How can I do it in a simple way (I mean, not to go through on all over the file and count each line)? Is there a command for that?
-
All over the file? We're talking about 3 lines of code to count this. – George Johnston Jan 20 '10 at 16:29
-
Does this answer your question? [Determine the number of lines within a text file](https://stackoverflow.com/questions/119559/determine-the-number-of-lines-within-a-text-file) – Josh Correia Oct 15 '20 at 23:12
4 Answers
Well:
int lines = File.ReadAllLines(path).Length;
is fairly simple, but not very efficient for huge text files. I'd probably use a TextReader
in most cases, to avoid excessive buffering:
int lines = 0;
using (TextReader reader = File.OpenText(path)) {
while (reader.ReadLine() != null) { lines++; }
}

- 1,026,079
- 266
- 2,566
- 2,900
-
tanks!!!! by the way, i used this for a file with 65500 lines, so i think it wors even for bug files... – aharon Jan 20 '10 at 16:42
Any "function" you call is going to essentially do the same thing -- go line by line and count the number of new line characters.
If you want to decieve yourself into making something seem more "crisp" a regex expression to count the number of new line characters would do the trick.

- 31,652
- 27
- 127
- 172
-
Once you've read the whole file in - not really feasible if it's big. Or you do it per line...oh wait... ;-) – Wim Jan 20 '10 at 16:29
-
Should we tell the OP that this will still read the whole file? ;) I suppose if there was metadata attached to the file that tracked the number of lines (it would probably only apply to text files) then you could just read that. But I don't think such a thing exists. – FrustratedWithFormsDesigner Jan 20 '10 at 16:34
One way or another (Regex, FileStream etc.) something, somewhere will have to go through the newlines.

- 11,998
- 1
- 34
- 57
Depends on the length of the line...usually the fastest way is to do this using this simple formula - get the length of the file in bytes and divided by the length of the line, for example:
const int LINE_LENGTH = 80; System.IO.FileInfo f = new System.IO.FileInfo(path); int noLines = f.Length / LINE_LENGTH;
The above assumes the length of the line is a constant 80 characters wide...
If the length is variable, there is no other way of doing it, apart from opening the file and iterate through each line and keep track of the line count.
Edit:
I just realized a much easier way after I posted a comment below..
Would it not be easier to actually use the Shell under System.Diagnostics.Process
namespace to invoke grep -c "$(\r?\n?|\n)"
using an argument as the path to the file in question - this would make it more flexible and can cater for any file. The only thing you would have to do is read in the output and regex it to get at the actual number of lines, for instance, using my grep that came with Borland C, I get this output
C:\Documents and Settings\Tom>grep -c "$(\r?\n?)" 273.txt File 273.txt: 35 lines match
The output and mileage may vary, the -c
command switch to grep means 'count of'

- 34,087
- 8
- 78
- 110
-
And it assumes ASCII or low-end UTF8 encoding, plus it doesn't account for the length of the newline character(s) themselves. – Marc Gravell Jan 20 '10 at 16:37
-
Hey, who is going to actually open a file with +65k lines, count each one, and then compare it to the results of this code? I say put it out there and change it if somebody submits a bug report. – Jan 20 '10 at 16:48
-
Or why not do a system shell process invoking grep -c "$(\r?\n)" to count the lines? You get the drift... ;) – t0mm13b Jan 20 '10 at 17:20