我希望在VB.NET中使用文本文件来验证用户名和密码。
我已经验证了用户名,但我无法验证密码,在txtpassowrd.text中输入的任何内容都将导致登录。
我使用的代码是:
Imports System.IO
Public Class frmReceptionist
Function IsInFile(ByVal person As String) As Boolean
If File.Exists("receptionistUser.txt") And File.Exists("receptionistPassword.txt") Then
Dim sr As StreamReader = File.OpenText("receptionistUser.txt")
Dim individual As String
Do Until sr.EndOfStream
individual = sr.ReadLine
If individual = person Then
sr.Close()
Return True
End If
Loop
sr.Close()
End If
Return False
End Function
Private Sub btnConfirm_Click(sender As Object, e As EventArgs) Handles btnConfirm.Click
'Determine if a person is in the file
Dim person As String = txtUsername.Text
If person <> "" Then
If IsInFile(person) Then
MessageBox.Show(person & " Welcome Receptionist", "Bia Duitse")
Me.Hide()
frmBiaDuitse.Show()
Else
MessageBox.Show(person & " Incorrect Login", "No")
End If
Else
MessageBox.Show("You must enter Details", "Information")
End If
txtUsername.Clear()
txtUsername.Focus()
End Sub
Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
Me.Hide()
frmSelectJob.Show()
End Sub
End Class发布于 2019-03-19 20:58:20
这绝对不是你应该这样做的方式。
出于学习目的,您可以将文件加载到Dictionary()中,如下所示:
Private Credentials As Dictionary(Of String, String)
Private Sub LoadCredentials()
If IsNothing(Credentials) Then
Credentials = New Dictionary(Of String, String)()
If File.Exists("receptionistUser.txt") And File.Exists("receptionistPassword.txt") Then
Dim users() As String = File.ReadAllLines("receptionistUser.txt")
Dim passwords() As String = File.ReadAllLines("receptionistPassword.txt")
If users.Length = passwords.Length Then
For i As Integer = 0 To users.Length - 1
Credentials.Add(users(i), passwords(i))
Next
End If
End If
End If
End Sub
Function IsInFile(ByVal person As String) As Boolean
LoadCredentials()
If Not IsNothing(Credentials) Then
Return Credentials.ContainsKey(person)
End If
Return False
End Function
Function Checkpassword(ByVal person As String, ByVal password As String) As Boolean
LoadCredentials()
If Not IsNothing(Credentials) Then
Return Credentials.ContainsKey(person) AndAlso password = Credentials(person)
End If
Return False
End Functionhttps://stackoverflow.com/questions/55240015
复制相似问题