none
Reading from specifc line RRS feed

  • Question

  • Hello, I wonder is possible to read specific lines in text file. For example line 2 and line 4. Thank you.
    Saturday, January 6, 2018 1:02 AM

Answers

  • Hi

    Here is an example:

    	' zero base array of strings
    	Dim lines() As String = IO.File.ReadAllLines("C:\Users\lesha\Desktop\Plans\New folder\Testing.txt")
    
    	' 3rd string - index 2
    	Dim line2 As String = lines(2)
    	' 5rd string - index 4
    	Dim line4 As String = lines(4)
    


    Regards Les, Livingston, Scotland

    • Proposed as answer by Frank L. Smith Saturday, January 6, 2018 1:17 AM
    • Marked as answer by BloodySandwich Saturday, January 6, 2018 1:20 AM
    Saturday, January 6, 2018 1:13 AM

All replies

  • Hello, I wonder is possible to read specific lines in text file. For example line 2 and line 4. Thank you.

    You can't access specific lines directly.

    You can read all lines into an array of lines and access each one directly from the array.
    https://msdn.microsoft.com/en-us/library/s2tte0y1(v=vs.110).aspx

    Or just read each line in turn and stop after you get the one you want.
    https://docs.microsoft.com/en-us/dotnet/standard/io/how-to-read-text-from-a-file

    • Proposed as answer by Frank L. Smith Saturday, January 6, 2018 1:17 AM
    Saturday, January 6, 2018 1:09 AM
  • Hi

    Here is an example:

    	' zero base array of strings
    	Dim lines() As String = IO.File.ReadAllLines("C:\Users\lesha\Desktop\Plans\New folder\Testing.txt")
    
    	' 3rd string - index 2
    	Dim line2 As String = lines(2)
    	' 5rd string - index 4
    	Dim line4 As String = lines(4)
    


    Regards Les, Livingston, Scotland

    • Proposed as answer by Frank L. Smith Saturday, January 6, 2018 1:17 AM
    • Marked as answer by BloodySandwich Saturday, January 6, 2018 1:20 AM
    Saturday, January 6, 2018 1:13 AM
  • Thanks guys for fast response, really appreciate.
    Saturday, January 6, 2018 1:21 AM
  • Although you have an answer, you should always assert that there are that many lines in the file. For instance, in the code below I look for the lines in question, I don't assume there are that many lines.

    Yes it's more lines of code but best to be safe.

    Imports System.IO
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim fileName As String = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TextFile1.txt")
    
            If File.Exists(fileName) Then
                Dim values As IEnumerable(Of String) = File.
                    ReadAllLines(fileName).
                    Select(Function(line, index)
                               If index = 1 OrElse index = 3 Then
                                   Return line
                               Else
                                   Return Nothing
                               End If
                           End Function).
                     Where(Function(item) item IsNot Nothing)
    
                If values.Count = 2 Then
                    MessageBox.Show($"{values(0)}, {values(1)}")
                Else
                    MessageBox.Show("Not enough lines, simply indexing would had thrown an exception")
                End If
            End If
    
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, January 6, 2018 2:18 AM
    Moderator