2013-11-03 226 views
0

我正在一个登录系统上,我使用VB创建程序。有两个问题与系统的时刻:第一个是,即使用户已经创建了一个账户,用户名和密码的文件出现在程序启动时进行擦拭;其次,当我尝试登录,它抛出了这个错误:VB登录系统资源问题

“类型‘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 

上述问题的任何帮助将是一个相当大的帮助。

+0

为什么不能读取文件一次全部进入(字符串)的列表(或列表(中UserPW)),以便你可以关闭这些文件。通过“抹”你的意思是文件是空的?至少,在离开表单之前,您还应该关闭您打开的文件。 Me.Hide将他们打开。 – Plutonix

+0

如果我使用me.close(),那么程序将终止,因为它是在启动时加载的表单。 –

回答

1

好:

你的文件正在被消灭的原因是,你是在声明他们的时间创建StreamWriter对象。 StreamWriter对象打开一个文件,并准备尽快为他们创建写微博,并有初始化模块级变量,只要父类实例化创建的,因此,正如你所看到的那样,首先发生的是您的文件被覆盖。

而且,由于打开文件进行写入需要锁定它,任何企图访问您的文件进行读操作将失败。这就是你得到这个错误的原因。

你真的不应该到目前为止提前使用它们的创建StreamWriter对象。你应该在最后时刻创建它们。你可以声明他们提前使用它们,但不要实例化他们(new他们),直到你需要他们,关闭或处置他们,只要你完成他们。

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

埃塔:这里的声明应该是什么样子:

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() 
+0

我已删除了声明的“新”的一部分,但是出现了一个错误:“数组边界不能出现在类型说明符” –

+1

'私人usernameWriter作为StreamWriter' – Plutonix

+0

@MarcusEagle我已经添加了正确的语法的例子。 –