VB を使用して作成しているプログラムのログイン システムに取り組んでいます。現在、システムには 2 つの問題があります。1 つ目は、ユーザーがアカウントを作成した場合でも、プログラムの起動時にユーザー名とパスワードのファイルが消去されているように見えることです。次に、ログインしようとすると、次のエラーがスローされます。
「タイプ 'System.IO.IOException' の未処理の例外が mscorlib.dll で発生しました
追加情報: 別のプロセスで使用されているため、プロセスはファイル '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
上記の問題に関するヘルプは、かなりの助けになります。