Streamreader错误循环:无法访问已处置的对象。 VB.NET

时间:2015-02-28 21:37:27

标签: vb.net loops streamreader

帮助!当我键入密码错误应用程序崩溃时出现以下错误:无法访问已处置的对象。该代码在VB.Net

Sub PassAuth()
    Dim checker As StreamReader = New StreamReader(WC.OpenRead(address))
    Do Until Authed = False


        readpass = checker.ReadLine

        If readline = "End" Then
            MsgBox("Incorrect Username 'Or Password", 0 + 16, "Error")
            Authed = False
            checker.Close()
            checker.Dispose()
            checker.ReadLine()
        ElseIf readline = usrbox.Text Then

            chekpass = checker.ReadLine


            If chekpass = passbox.Text Then
                Authed = False
                checker.Close()
                checker.Dispose()
                AccessGranted()
            End If

        End If
    Loop
    Authed = True
     checker.Close()
     checker.Dispose()

出现错误时:

 chekpass = checker.ReadLine

我从未处理过流,为什么会发生这种情况。

编辑:它读取用户名文件,如果它检测到用户名它将启动PassAuth Sub。 PassAuth Sub检查每一行如果它检测到用户名它检查下一行密码。如果密码匹配它不会崩溃,如果他们不匹配它将崩溃。如果它没有检测到任何匹配的密码,它将会到达终点线。什么时候它会说错密码。这是UserAuth Sub:

Dim reader As StreamReader = New StreamReader(WC.OpenRead(address))
    Do Until usrauth = False

        readline = reader.ReadLine
        If readline = "End" Then
            UsrAuth = False
            MsgBox("Incorrect Username Or Password", 0 + 16, "Error")
            reader.Dispose()
            reader.Close()
        ElseIf readline = usrbox.Text Then
            UsrAuth = False
            reader.Dispose()
            reader.Close()
            PassAuth()


        End If
    Loop
     reader.Dispose()
    reader.Close()

请注意,这会访问FTP服务器。

1 个答案:

答案 0 :(得分:1)

我会使用File.ReadAllLines()

简化您的代码
Sub PassAuth()
    Dim lines = File.ReadAllLines(address)
    if lines(0) = usrbox.Text AndAlso lines(1) = passbox.Text Then
       AccessGranted()
    else
       MsgBox("Incorrect Username 'Or Password", 0 + 16, "Error")
    End If
End Sub