VB登录系统资源问题

时间:2013-11-03 19:11:32

标签: vb.net login

我正在使用VB创建程序的登录系统。目前系统存在两个问题:第一个问题是,即使用户创建了帐户,程序启动时也会擦除用户名和密码文件;其次,当我尝试登录时,它会抛出此错误:

“mscorlib.dll中发生了'System.IO.IOException'类型的未处理异常

附加信息:进程无法访问文件'J:\ Computing Coursework \ real project \ KES \ Resources \ username.txt',因为它正由另一个进程使用。“

系统代码如下:

    Imports System.IO

Public Class Login

Private Sub Login_Load(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub

Private usernameWriter As New StreamWriter("J:\Computing Coursework\real project\KES\Resources\username.txt") 'Creates the stream for writing the username to file

Private passwordWriter As New StreamWriter("J:\Computing Coursework\real project\KES\Resources\password.txt") 'Creates the stream for writing the password to file

Dim currentLogin As String 'Allows the program to recognise which user is logged in currently

Private Sub btn_CreateAccount_Click(sender As Object, e As EventArgs) Handles btn_CreateAccount.Click
    usernameWriter.WriteLine(txtbox_UsernameCreate.Text) 'Writes the username to the username file.
    passwordWriter.WriteLine(txtbox_PasswordCreate.Text) 'Writes the password to the password file.
    usernameWriter.Close() 'Closes the username file after writing to it so that changes are saved to the file.
    passwordWriter.Close() 'Closes the password file for the same reason as the username file above.
    currentLogin = txtbox_UsernameCreate.Text
    Dim statsFile As New StreamWriter("J:\Computing Coursework\real project\KES\Resources\" + currentLogin + ".txt")
    Tokyo.Show() 'Tokyo is the name for the main menu
    Me.Hide() 'As Login is the startup form for this solution, it is hidden instead of closed so that the program will not terminate when the login screen dissapears.
End Sub

Private Sub btn_Login_Click(sender As Object, e As EventArgs) Handles btn_Login.Click
    Dim usernameReader As New StreamReader("J:\Computing Coursework\real project\KES\Resources\username.txt") 'Sets the location for the username to be found in

    Dim passwordReader As New StreamReader("J:\Computing Coursework\real project\KES\Resources\password.txt") 'Sets the location for the password to be found in

    Dim n As Integer
    Dim i As Integer
    While n < 101 'Checks the first 100 lines for the username
        If txtbox_UsernameCreate.Text = usernameReader.ReadLine Then 'If the username is found close the usernameReader and move on to the password
            usernameReader.Close()
            While i < 101 'checks the first 100 password entries
                If txtboxPassword.Text = passwordReader.ReadLine Then 'If the password is found then close the passwordReader, set the login ID and then open the main menu
                    passwordReader.Close()
                    currentLogin = txtbox_UserName.Text
                    Me.Hide()
                    Tokyo.Show()
                Else
                    i += 1 'otherwise it increments the count so that the next line can be read
                End If
                MsgBox("No valid password") 'If the first 100 lines have been checked and there is no password then this returns the msgbox
            End While
        Else
            n += 1 'otherwise it increments the count so that the next line can be read
        End If
        MsgBox("No valid username") 'If the first 100 lines have been checked and there is no username then this returns the msgbox
    End While
End Sub
End Class

对上述问题的任何帮助都会有相当大的帮助。

1 个答案:

答案 0 :(得分:1)

好:

您的文件被清除的原因是您在声明它们时创建了StreamWriter个对象。 StreamWriter个对象打开一个文件并准备在创建文件后立即写入它,并且一旦父类被实例化,就会创建具有初始化程序的模块级变量,因此,如您所见,第一个发生的事情是你的文件被覆盖。

此外,由于打开要写入的文件需要将其锁定,因此任何访问您的文件以进行读取的尝试都将失败。这就是你得到这个错误的原因。

到目前为止,您确实不应该在使用它们之前创建StreamWriter个对象。你应该在最后一刻创建它们。您可以在使用它们之前声明,但不要实例化它们(new它们)直到您需要它们,并尽快关闭或处理它们因为你已经完成了它们。

(同样的原则 - 在最后时刻创造,在使用后处置 - 也适用于读者。)

ETA:这是声明的样子:

 Private usernameWriter As StreamWriter  'The stream for writing the username to file
 Private passwordWriter As StreamWriter  'The stream for writing the password to file

然后,当它需要使用它们时,创建它们,使用它们并关闭它们,如下所示:

 usernameWriter = new StreamWriter("J:\Computing Coursework\real project\KES\Resources\username.txt")
 usernameWriter.WriteLine(txtbox_UsernameCreate.Text) 'Writes the username to the username file.
 usernameWriter.Close()

 passwordWriter = new StreamWriter("J:\Computing Coursework\real project\KES\Resources\password.txt")
 passwordWriter.WriteLine(txtbox_PasswordCreate.Text) 'Writes the password to the password file.
 passwordWriter.Close()