No System.IO.File.ReadLines (file) .Count () in .Net 4?

I'm trying to read the number of lines of a file. I found here (stackoverflow) that the best way to read the number of lines in a large file is to use the following code:

int count = System.IO.File.ReadLines(file).Count();

However, I can not compile it. Does anyone know what the problem is?

Error 5 'System.Collections.Generic.IEnumerable<string>'does not contain the definition of “Count” and the extension method “Count”. Taking the first argument of the type 'System.Collections.Generic.IEnumerable<string>'can be found (do you have a using directive or an assembly reference?)

Thank you Eyal

+5
source share
3 answers

Count<T>()is an extension method for objects IEnumerable<T>. Try adding a statement usingfor the namespace System.Linq.

+12
source

Could you do:

int count = File.ReadAllLines(@"C:\filepath\file.txt").Length;

EDIT: , () . , , view

+3

This is the counter for the line from the application window when reading from a text file:

int linecount = System.IO.File.ReadAllLines(@"D:\yfile.txt").Length;
MessageBox.Show(linecount != null && (!string.IsNullOrEmpty(linecount.ToString())) ? linecount.ToString() : "Unable To Count");
0
source

All Articles