2013-11-03 225 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我已經添加了正確的語法的例子。 –