Built-in Function for finding out the Number of Lines In a Text File

Hi,

In my application , I'm having a requirement to find the number of lines in a particular text (.txt) file. Is there any built-in File-Handling function to do this

Or can we read the content of the file to a string variable and then is there any String manipulation functions to find out the number of lines in a file.

Or , there is no other go , we should only have a loop and then find out the number of lines in using some logic. But by doing so , in case of a large file may degrade the performance of the application.

Kindly suggest me some solution for this !

Thanks in Advance,

Lokesh R



Answer this question

Built-in Function for finding out the Number of Lines In a Text File

  • Chrisull

    to find the number of lines in a file, I believe you would need to read it line by line. Example:

    Dim theStreamReader as new StreamReader("filename.txt")

    dim lines as Integer = 0

    Dim theStringBuilder as new StringBuilder()

    while theStreamReader.EndOfStream = false

       theStringBuilder.Append(theStreamReader.ReadLine())

       lines = lines + 1

    end while

    theStreamReader.Close()

     

    this will hold the contents of file into the StringBuilder, if you wanted, but also increase the lines variable by one for every line it reads until no more lines left

    does this help



  • Built-in Function for finding out the Number of Lines In a Text File