You need to give the loop variable a type, so change
foreach(line in lines)
to
foreach(string line in lines)
Apart from that you will get another exception:
ObjectDisposedException : {"Cannot read from a closed TextReader."}
File.ReadLines
does not return a collection, it is similar like a StreamReader
, it allows to read the file without reading all into memory first. But it needs an open stream. Once you have "executed" it it is disposed.
So you either have to use File.ReadAllLines
which returns a string[]
or don't use it twice.
Count()
foreach(line in lines)
From MSDN:
The
ReadLines
andReadAllLines
methods differ as follows: When you use ReadLines, you can start enumerating the collection of strings before the whole collection is returned; when you use ReadAllLines, you must wait for the whole array of strings be returned before you can access the array. Therefore, when you are working with very large files, ReadLines can be more efficient.