我想从文本文件中加载字符串作为变量。例如,一个不安全的登录屏幕,用户将其凭证输入到文本框中,程序将读取已输入到文本框中的内容,并且当文本文件中存储的凭证相同时将加载下一个表单。
答案 0 :(得分:0)
更新:解决了超过1个半小时的谷歌搜索
Public Class Form1
Public pass As String = String.Empty
Public user As String = String.Empty
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If TextBox2.Text = (user) And TextBox1.Text = (pass) Then
Login.Show()
Me.Close()
Else
MessageBox.Show("Wrong Password or User Name")
End If
TextBox2.Text = ""
TextBox1.Text = ""
End Sub
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
If TextBox2.Text = "" And TextBox1.Text = "" Then
Button1.Enabled = False
Else
Button1.Enabled = True
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
pass = IO.File.ReadAllText("pass.txt")
user = IO.File.ReadAllText("user.txt")
End Sub
End Class
刚刚使用了IO.File.ReadAllText(“user.txt”)这将读取文本文件中的所有字符串,并将它们存储到空变量中,而不是最好的方法,但适用于此应用程序