Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim FILE_NAME As String = Cashierpath
System.IO.File.Exists(FILE_NAME) ' current
Dim objReader As StreamReader
Dim user As String = TextBox1.Text
Dim password As String = TextBox2.Text
Dim check As String
'Global Variable
'Dim DirPath7 As String = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Scrap Data\Cashier Info\Cashiers\")
For Each filename As String In IO.Directory.EnumerateFiles(DirPath7, "*.txt")
Dim fName As String = IO.Path.GetFileName(filename)
If user = fName & ".txt" Then
objReader = New StreamReader(fName)
check = objReader.ReadToEnd()
If password = check Then
MessageBox.Show("Welcome " & user & "!")
Close()
My.Forms.Home.Show()
Else
MessageBox.Show("Username or Password is incorrect")
End If
End If
Next
End Sub
当用户在文本框中输入“用户名”和“密码”,然后单击此按钮时,我希望此按钮检查是否有输入用户名的文本文件,以及是否有文件那个用户名必须读取它并检查密码是否与文件中的字符串匹配。如果它不匹配,它必须显示一个消息框,说“用户名或密码不正确”,但是当我点击这个按钮时没有任何反应。也没有出现错误消息。 有人可以看看我的代码并告诉我我做错了什么?
答案 0 :(得分:1)
您所拥有的是一种处理用户凭据的糟糕方式!
阅读本文以获取更多信息:Salted Password Hashing - Doing it Right
无论如何,你过度编码了。
在你的应用程序的其他地方(正确使用Combine):
' Global Variable
Friend Shared DirPath7 As String = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, "Scrap Data", "Cashier Info", "Cashiers")
按钮处理程序:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim User As String = TextBox1.Text.Trim
Dim Pass As String = TextBox2.Text.Trim
' Assemble the full expected file path, even if it might be malformed.
' The first case check protects against malformed path.
Dim FilePath As String = IO.Path.Combine(DirPath7, String.Format("{0}.txt", User))
Select Case True
Case User.Length = 0
MsgBox("Username is empty", vbExclamation, "Error")
Case Pass.Length = 0
MsgBox("Password is empty", vbExclamation, "Error")
Case Not IO.File.Exists(FilePath)
MsgBox("No file for User", vbExclamation, "Error")
Case Not IO.File.ReadAllText(FilePath) = Pass
MsgBox("Wrong Password", vbExclamation, "Error")
Case Else
MsgBox(String.Format("Welcome {0}!", User), vbOKOnly, "Success")
My.Forms.Home.Show()
End Select
End Sub
答案 1 :(得分:0)
Dim objReader As StreamReader
Dim user As String = TextBox1.Text
Dim password As String = TextBox2.Text
Dim check As String
Dim fname = Path.Combine(DirPath7, String.Format("{0}.txt", user))
If File.Exists(fname) Then
Using objreader As New StreamReader(fname)
'objReader = StreamReader(fname)
check = objreader.ReadToEnd()
password = check
MessageBox.Show("Welcome " & user & "!")
Close()
My.Forms.Home.Show()
End Using
Else : MessageBox.Show("file not found, no user exists")
End If
删除了额外的“。txt”
添加“使用”。 。 。“结束使用”