0
我已經看到這個回答多次,我想我知道問題是什麼,但在我的情況下,解決方案對我來說沒有意義。在mscorlib.dll中發生未處理的異常類型'System.IO.IOException'
我收到以下錯誤:
An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll
Additional information: The process cannot access the file 'C:\Users\llexl\Desktop\Test\Encrypted String.txt' because it is being used by another process.
我分開的代碼的兩個部分到他們自己的按鈕來解決,這是負責創建文件的第一部分工作得很好,但在第二部分負責編輯文件並重命名它們的代碼是當發生這種錯誤時對我來說沒有意義,因爲我知道System.IO.File.Create
應該不需要關閉。
反正這裏是多數民衆贊成給我的麻煩代碼:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
TextBox2.Text = FolderBrowserDialog1.SelectedPath
TextBox2.AppendText("\Encrypted String.txt")
System.IO.File.Create(TextBox2.Text)
TextBox3.Text = FolderBrowserDialog1.SelectedPath
TextBox3.AppendText("\masterkey.txt")
System.IO.File.Create(TextBox3.Text)
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim file As System.IO.StreamWriter
Dim file2 As System.IO.StreamWriter
file = My.Computer.FileSystem.OpenTextFileWriter(TextBox2.Text, True)
file.Write(RichTextBox1.Text)
file.Close()
file2 = My.Computer.FileSystem.OpenTextFileWriter(TextBox3.Text, True)
file2.Write(TextBox1.Text)
file2.Close()
My.Computer.FileSystem.RenameFile(TextBox3.Text, "masterkey.perlpron")
My.Computer.FileSystem.RenameFile(TextBox2.Text, "Encrypted String.perlpron")
MsgBox("Library ready", MsgBoxStyle.Information)
End Sub
我會很感激一些幫助:)
我想補充一個.Dispose()來創建句子,只是以... _System.IO.File.Create(TextBox2.Text).Dispose()_ –
那麼,你可能知道你的程序行爲方式至少有20%是錯誤的。您可以通過查看MSDN中的File.Create文檔來使其達到100%。它是一個函數,其獨特之處在於它的返回值必須從不被忽略。這不僅是缺陷,它也是一個非常糟糕的做法。如果您的程序在完成寫入文件之前因任何原因而結束,那麼您剛剛離開了地雷,準備在任何期望該文件包含* something *的毫無預期的程序中爆炸。不清楚你爲什麼要這樣做,只有合理的選擇是File.Delete。 –