Read From txt file with VB.NET? What's different?
VB.NET is not liking my code to open a text file for read access. I'm trying to populate a listbox with the contents of a text file, but .NET does not recognize the following code:
Code:
Dim strFile As String
strFile = "\support files\chronill.dat"
Open strFile for input as #1
What is different in VB.NET from 6.0 as far as this goes?
Re: Read From txt file with VB.NET? What's different?
See the System.IO namespaces it contains all the classes you need...
Regards
Jorge
Re: Read From txt file with VB.NET? What's different?
You need to use a streamreader to read from a file.
VB Code:
Imports System.IO
Private Sub btnReadFromFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadFromFile.Click
Dim myStreamReader As StreamReader
myStreamReader = File.OpenText(txtFileName.Text)
Me.txtFileText.Text = myStreamReader.ReadToEnd()
End Sub
Re: Read From txt file with VB.NET? What's different?
In .NET there is a totaly new way to read from file, StreamReader
VB Code:
Dim AReadedLine As String ' The buffer string that will contain the strings read
Dim MyStreamReader As System.IO.StreamReader = New System.IO.StreamReader("MyFileName") ' Open the file
AReadedLine = MyStreamReader.ReadLine() ' Read a line from the file