Link to home
Start Free TrialLog in
Avatar of grahamd52
grahamd52

asked on

VB 2010 reading a text file line by line

Hi, cannot use "line input #n, string.variable" anymore to read a text file line by line in VB 2010. There are several methods, none of which seem to work including the below.
Can you suggest a simple method that does work.

The below when debug mode was started did nothing. The text file had three lines in it, "line 1", "Line 2" & "Line 3".

Imports System
Imports System.IO
Imports System.Text
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim fileReader As System.IO.StreamReader
        fileReader = My.Computer.FileSystem.OpenTextFileReader("C:\myfiles\test.txt")
        Dim stringReader As String
        stringReader = fileReader.ReadLine()
        MsgBox("The first line of the file is " & stringReader)
    End Sub
End Class
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Avatar of grahamd52
grahamd52

ASKER

I tried that taking out the bit that writes the file but it would not build giving an error on the exception variable e saying "Variable 'e' hides a variable in an enclosing block.
Even when the exception error is cleared, it still does nothing.
can you show your code?
Here it is.

Imports System
Imports System.IO
Imports System.Text
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim path As String = "c:\myfiles\Test.txt"

        Try
            Dim sr As StreamReader = New StreamReader(path)

            Do While sr.Peek() >= 0
                MsgBox(sr.ReadLine())
            Loop
            sr.Close()
        Catch ex As Exception
            MsgBox("The process failed: {0}", e.ToString())
        End Try
    End Sub
End Class
SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes it worked, thank you.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial