0

Hello
I would like to read a file and display certain lines ( like from line 5 to 10) in a label. i know how to read the file line by line but not the specified line i want.

Thanks in advance

5
Contributors
8
Replies
17
Views
7 Years
Discussion Span
Last Post by wasyazwan
0

Read the file line by line and when you get to the line you want, stop. Might need an if statement

0

I am not so sure what you mean. My current code is:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim myfile As String = "C:\Users\londonG\Documents\lillefile.txt"
        
Dim TextLine As String

        If System.IO.File.Exists(myfile) = True Then

            Dim objReader As New System.IO.StreamReader(myfile)

            Do While objReader.Peek() <> -1
                TextLine = TextLine & objReader.ReadLine() & vbNewLine
            Loop
            TextBox1.Text = TextLine
        
        End If
    End Sub

Where should i put the if statement. Is it inside the loop?
How should I write it?
Thank you

Edited by WaltP: Added CODE tags -- with all the help about them, how could you miss using them????

0

I am not so sure what you mean. My current code is:

Maybe you should have posted this last time... :icon_wink:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim myfile As String = "C:\Users\londonG\Documents\lillefile.txt"
        
Dim TextLine As String

        If System.IO.File.Exists(myfile) = True Then

            Dim objReader As New System.IO.StreamReader(myfile)

            Do While objReader.Peek() <> -1
                TextLine = TextLine & objReader.ReadLine() & vbNewLine
            Loop
            TextBox1.Text = TextLine
        
        End If
    End Sub

Where should i put the if statement. Is it inside the loop?
How should I write it?
Thank you

Are you asking me to write it for you? What does your code do currently? Where do you think the if should go?

0

and perhaps Walt you should recognize that this thread is in the wrong forum and have moved it to the VB.NET forum...

0

and perhaps Walt you should recognize that this thread is in the wrong forum and have moved it to the VB.NET forum...

Since I don't know VB.net, maybe I wouldn't... :icon_wink:

I'll move it, though...

0

Thanks WaltP.

>I would like to read a file and display certain lines ( like from line 5 to 10) in a label.

ReadAllLines() method of File class will solve your problem.

Dim lines() As String = System.IO.File.ReadAllLines("file_path")
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.