所有语法都是正确的,当我运行代码时没有出现错误,但是当我单击表单上的登录按钮时,它只是冻结,我认为这是由于数组。 这是下面的代码。我不知道如何使其高效使其不冻结。
Imports System.IO
Public Class Form3
Dim reader1 As System.IO.StreamReader
Dim classdata As Array
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
reader1 = My.Computer.FileSystem.OpenTextFileReader("C:\Users\ryanm_000\Documents\username and password.txt")
While reader1.EndOfStream = True
classdata = reader1.ReadLine.Split(",")
If TextBox1.Text = "admin" AndAlso TextBox2.Text = "iamtheboss" Then
MsgBox("Welcome Mr Khan", MsgBoxStyle.Information, "Admin")
Me.Hide()
My.Forms.Form7.Text = Now.ToString
My.Forms.Form7.Show()
ElseIf TextBox1.Text = classdata(0) AndAlso TextBox2.Text = classdata(1) Then
Me.Hide()
My.Forms.Form5.Text = Now.ToString
My.Forms.Form5.Show()
reader1.Close()
Else
MsgBox("Sorry, username or password is invalid, please try again", MsgBoxStyle.Information, "Error")
End If
End While
End Sub
End Class
答案 0 :(得分:2)
您希望在阅读器不在流的末尾时处理文件。
尝试将您的while条件更改为While reader1.EndofStream = False
或(更好)您可以写While Not reader1.EndOfStream