Question How to match TextBox values and Text file strings/lines?

Lancer26

New member
Joined
Oct 8, 2015
Messages
2
Programming Experience
Beginner
I created this account just to ask this question XD.

So, I'm currently working on a school project. The project requires ONLY the use of Visual Basic. So, no MS Access or anything. (Maybe a few.. text files.)

The first few forms of my program shows a log-in screen, that uses the "If, Then, Else" for detecting the correct username and password.

I decided to add a feature to my program. It lets the user fill-up two textboxes(Username and Password), once that's done, the program writes the credentials into a text file. So now there are two, the Register Form and the Log-In Form. Here's my code on it, if that could help:

VB.NET:
Dim file As System.IO.StreamWriter
    file = My.Computer.FileSystem.OpenTextFileWriter("d:/users.txt", True)
    file.WriteLine(TextBox1.Text + "," + TextBox2.Text)
    file.Close()
    MessageBox.Show("User Created")
    Me.Hide()
    frmLogin.Show()

On the Log-In form, two textboxes(Username, Password) are placed. The program SHOULD use the value of the two text boxes, compare them to the lines of the text file(Maybe using Do While-Loop?), find a match and proceed to the program.

Now, I asked this same question on another forum. People who replied suggested something like splitting them into an array and ReadAllLines. But due to life problems, I wasn't able to test it out, I able to think of a code in my head, but not in a computer.

Btw, in my case, what's better? ReadAllLines or use some code to test each line?

Here's my INCOMPLETE code that I thought of.

VB.NET:
Dim userpass As String
Dim filereader As System.IO.StreamReader
filereader = My.Computer.FileSystem.OpenTextFileReader("D:/users.txt")

Do While filereader.Peek() <> -1
	userpass= filereader.ReadLine()
		userpass.Split("","")

In what way can I continue the code?

PS: I have Visual Basic 2010. If that would matter.
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,439
Location
Norway
Programming Experience
10+
Btw, in my case, what's better? ReadAllLines or use some code to test each line?
It doesn't matter, but you can reduce the test to just one line of code using ReadAllLines, for example:
        Dim cred = "testuser,testpass" 'sample input to test against file
        If IO.File.ReadAllLines("D:/users.txt").Contains(cred) Then
            'valid credentials
        End If

This is simple testing for the combined user/pass value, in a more real world scenario you would probably want to give feedback if for example the user name didn't exist or just the password was wrong. You would also rarely store credentials in plain text, especially password should be hashed, but that is more advanced.
 
Top Bottom