How to count the lines in text file with C#

1 Answer

0 votes
using System;
using System.IO;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(CountLinesInFile("d:\\data.txt"));
        }
        static long CountLinesInFile(string filename)
        {
            long count = 0;
            using (StreamReader sr = new StreamReader(filename))
            {
                string line;
                while ((line = sr.ReadLine()) != null)
                    count++;
            }
            return count;
        }
    }
}


/*
run:
 
3

*/

 



answered Mar 11, 2017 by avibootz

Related questions

1 answer 192 views
2 answers 267 views
1 answer 196 views
196 views asked Jan 20, 2017 by avibootz
1 answer 177 views
177 views asked Mar 12, 2015 by avibootz
1 answer 174 views
1 answer 215 views
...