Results 1 to 5 of 5

Thread: Get Specific Text File Line

Hybrid View

  1. #1
    Hyperactive Member
    Join Date
    Dec 11
    Posts
    346

    Get Specific Text File Line

    Ive been looking up on google and I cant seem to get anything. I know how to read a text file but I want to just get the second line. How would this be done?

  2. #2
    Hyperactive Member marniel647's Avatar
    Join Date
    Aug 10
    Location
    MSDN Library
    Posts
    256

    Re: Get Specific Text File Line

    You can do loop while reading line by line and stop the loop when you get in the second line

  3. #3
    Hyperactive Member
    Join Date
    Dec 11
    Posts
    346

    Re: Get Specific Text File Line

    How would I check that its on the second line?

  4. #4
    Frenzied Member MattP's Avatar
    Join Date
    Dec 08
    Location
    WY
    Posts
    1,182

    Re: Get Specific Text File Line

    Check out the StreamReader.ReadLine Method. Call it once and do nothing with the results and the 2nd call will get you the 2nd line from the file.
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,747

    Re: Get Specific Text File Line

    You can't just get a specific line. To get line N you must then you have to read every line before that and, if you don't need them, just discard them.
    vb.net Code:
    1. Private Function GetLineFromFile(filePath As String, lineNumber As Integer) As String
    2.     Using reader As New IO.StreamReader(filePath)
    3.         Dim line As String = Nothing
    4.  
    5.         For i = 1 To lineNumber
    6.             line = reader.ReadLine()
    7.  
    8.             If line = Nothing Then
    9.                 'No more lines
    10.                 Exit For
    11.             End If
    12.         Next
    13.  
    14.         Return line
    15.     End Using
    16. End Function

Posting Permissions

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