Get Line Count from a File

B

Brian Simmons

Hi All,

I'm hoping there's a quick and easy to accomplish this:
I've got a CSV file (each row represents a record, and is followed by a
NewLine).
Is there any way (short of counting the NewLine's) to get a Line Count of
the file.

I've got a quick & dirty utility that I wrote for a client, and right now,
they run the utility and it checks for the existence of the CSV file. If
it's found they get a messagebox like
"File exists, task needs to be run"
I would like in addition for checking for the file's existence, get the line
count and present a message like:
"File exists, task needs to be run, and there are XX records to process"

Thanks,
Brian
 
J

Jon Skeet [C# MVP]

I'm hoping there's a quick and easy to accomplish this:
I've got a CSV file (each row represents a record, and is followed by a
NewLine).
Is there any way (short of counting the NewLine's) to get a Line Count of
the file.

Well, I wouldn't count the newlines by hand. I'd either call
TextReader.ReadAllLines and return the length of the array, or (if I
thought the file might be largish) keep a count and call ReadLine
until it returned null.

Jon
 
B

Brian Simmons

Perfect Jon, works like a charm. Thanks!

Jon Skeet said:
Well, I wouldn't count the newlines by hand. I'd either call
TextReader.ReadAllLines and return the length of the array, or (if I
thought the file might be largish) keep a count and call ReadLine
until it returned null.

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top