I want to read a text file that has 3 lines of data one line contains “Server = …” The next line is “Username = .. ” and the last line is “Password = … ” So I want to read this file and put each line into a separate label.. (label1,label2,label3)
OFDSet is my openfiledialog
All in Visual Basic..
Some one help?
Here is the code I tried but I get an error:
Dim oReader As StreamReader
If OFDSet.ShowDialog = Windows.Forms.DialogResult.OK Then
oReader = New StreamReader(OFDSet.FileName, True)
ServLabel.Text = oReader.ReadLine(1)
UserLabel.Text = oReader.ReadLine(2)
PassLabel.Text = oReader.ReadLine(3)
End If
A simpler solution is through the File.ReadAllLines method that gives back an array of strings with just one call.
See the MSDN reference here
(File class needs an imports for System.IO)
Also if your first line is something like this
and you want only the part
MyServerPCin the ServLabel you need to split the input at the=signOf course this is just an example. More robust error handling is needed. You should check if you have at least 3 lines and if each line is correctly separated by the
=sign.