.txt file

Talk2Tom11

Well-known member
Joined
Feb 22, 2004
Messages
144
Location
Westchester, NY
I want to open a .txt file and just get the number of lines that it contains.
Not the specific information within the lines. But just a line count. How would i go about that?
 
Assuming this is in a method called something meaningful like GetNumberOfLines(byval fileName As String)...From the hip:
Code:
Dim sr as StreamReader = Nothing

Try
   sr  = New StreamReader(fileName)
   Dim numberOfLines As Integer = 0

   read the line, dont care about the value so dont bother storing it
   While Not sr.ReadLine() Is Nothing
      numberOfLines += 1
   End While

   return numberOfLines
Catch ex As Exception
   something went wrong
Finally
   If not sr Is Nothing Then
      sr.Close()
   End If

   sr = nothing  paranoid
End Try
 
Back
Top