如何从文本文件中读取

时间:2017-10-20 21:46:06

标签: vb.net readfile

如何阅读用户名和密码以便输入登录信息?我在外部文件中有用户名和密码,但是当我运行程序并输入正确的文件时,它就会出现"抱歉,用户名或密码无效,请再试一次"我提出的错误信息出现了。

Public Class Form3
Dim filereader1 As System.IO.StreamReader
Dim filereader2 As System.IO.StreamReader
Dim username2, password1 As String
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If TextBox1.Text = Nothing Then
        MsgBox("You need to enter your username and password in the textboxes", MsgBoxStyle.Information, "Error")
    ElseIf TextBox2.Text = Nothing Then
        MsgBox("You need to enter your username and password in the textboxes", MsgBoxStyle.Information, "Error")
        Me.Show()
        filereader1 = My.Computer.FileSystem.OpenTextFileReader("C:\Users\ryanm_000\Documents\username.txt")
        filereader2 = My.Computer.FileSystem.OpenTextFileReader("C:\Users\ryanm_000\Documents\password.txt")
        username2 = filereader1.ReadLine
        password1 = filereader2.ReadLine
    ElseIf TextBox1.Text = username2 And TextBox2.Text = password1 Then
        Me.Hide()
        My.Forms.Form5.Text = Now.ToString
        My.Forms.Form5.Show()
    Else
        MsgBox("Sorry, username or password is invalid, please try again", MsgBoxStyle.Information, "Error")

    End If


End Sub


End Class

1 个答案:

答案 0 :(得分:1)

您的条件风格不正确。你必须做这种风格。此外,您可以使用一些更好的方法来创建登录页面,例如使用数据库或XML文件。

If TextBox1.Text = Nothing Or TextBox2.Text = Nothing Then
                MsgBox("You need to enter your username and password in the textboxes", MsgBoxStyle.Information, "Error")
            Else
                filereader1 = My.Computer.FileSystem.OpenTextFileReader("C:\Users\ryanm_000\Documents\username.txt")
            filereader2 = My.Computer.FileSystem.OpenTextFileReader("C:\Users\ryanm_000\Documents\password.txt")
            username2 = filereader1.ReadLine
            password1 = filereader2.ReadLine

            If TextBox1.Text = username2 And TextBox2.Text = password1 Then
                Me.Hide()
                My.Forms.Form5.Text = Now.ToString
                My.Forms.Form5.Show()
            Else
                MsgBox("Sorry, username or password is invalid, please try again", MsgBoxStyle.Information, "Error")

            End If
        End If