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.Ope nTextFileR eader("C:\ myfiles\te st.txt")
Dim stringReader As String
stringReader = fileReader.ReadLine()
MsgBox("The first line of the file is " & stringReader)
End Sub
End Class
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.Ope
Dim stringReader As String
stringReader = fileReader.ReadLine()
MsgBox("The first line of the file is " & stringReader)
End Sub
End Class
there is an example at https://msdn.microsoft.com/en-us/library/system.io.streamreader.readline(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2
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.
ASKER
Even when the exception error is cleared, it still does nothing.
can you show your code?
ASKER
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yes it worked, thank you.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.