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

Recommended Answers

All 8 Replies

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

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

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?

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

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...

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")

Ok,
Thank you very much for your help. I done it now :-)

Please review my thread. It will help you in this problem...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.