Results 1 to 4 of 4

Thread: Simple question (I Hope)

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2002
    Location
    Anywhere but here
    Posts
    161

    Smile Simple question (I Hope)

    All im trying to do is make this function to return how many lines are in the log file. I had this working perfect in vb6 and had some NIMROD idea to try and learn some .NET and now im sort of stuck on it.

    This is what Ive come up with so far but all that happens is it seems to loop forever.
    VB Code:
    1. Public Function LogSize(ByVal pLog As String) As Long
    2.  
    3.         Dim x As Long
    4.         Dim sTmp As String
    5.         Dim oFile As New System.IO.FileStream(pLog, IO.FileMode.Open)
    6.         Dim oRead As New System.IO.StreamReader(oFile)
    7.  
    8.         Do While oRead.Read
    9.             sTmp = oRead.ReadLine
    10.             x = x + 1
    11.         Loop
    12.         LogSize = x
    13.         oRead.Close()
    14.     End Function

    Thanks for looking
    -------------------------
    My name says it all!

  2. #2
    Addicted Member
    Join Date
    Jun 2003
    Location
    Birmingham, AL
    Posts
    188
    I'm at work right now an can't check to see if this is right but try it an see

    VB Code:
    1. Public Function LogSize(ByVal pLog As String) As Long
    2.         Try
    3.             Dim sr As StreamReader = New StreamReader(pLog)
    4.             Dim line As String, lines As Long
    5.             line = sr.ReadLine()
    6.             Do
    7.                 lines += 1
    8.                 line = sr.ReadLine()
    9.             Loop Until line Is Nothing
    10.             sr.Close()
    11.             return lines;
    12.         Catch e As Exception
    13.             ' In case of error
    14.             MessageBox.Show("Exception Occurred: " & vbcrlf & e.Message)
    15.             Return -1;
    16.         End Try
    17.     End Function

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You can also use split instead of looping:
    VB Code:
    1. Private Function GetLineCount(ByVal filepath As String) As Integer
    2.         Dim sr As New IO.StreamReader(filepath)
    3.         Dim data As String = sr.ReadToEnd
    4.         sr.Close()
    5.         Return data.Split(ControlChars.NewLine).Length
    6.     End Function

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Apr 2002
    Location
    Anywhere but here
    Posts
    161
    Thanks both for the input... .Net is driving me nuts.
    -------------------------
    My name says it all!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width