C# Code to count number of lines

I am posting this code because i saw many programmers asking "How to count number of lines"

Call below function by passing the file name with the path (e.g:- c:\\Test.txt)as string.

Below function will return number of lines in the file.


public int LineCount(string strFileName)
{
StreamReader re = File.OpenText(strFileName);
int intCount=0;
string input=null;
while ((input = re.ReadLine()) != null)
{
if(!(input.Trim()==string.Empty))
{
intCount+=1;
}
}
re.Close();
return intCount;
}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: