0

我在处理这段代码时遇到了问题。StreamWriter 创建 .txt 文档并将其放入项目 bin 文件夹中,但是当我运行该程序时,我收到一条错误消息:

mscorlib.dll
中发生了“System.IO.IOException”类型的未处理异常,进程无法访问该文件……因为它正被另一个进程使用。

我的计算机上没有运行其他程序,所以我不确定其他进程可能正在使用它。这是我的代码,在此先感谢。

Private Sub saveButton_Click(sender As Object, e As EventArgs) Handles saveButton.Click
    Dim voteWriter As IO.StreamWriter
    Dim vote As String

    voteWriter = IO.File.AppendText("warrenHS.txt")
    vote = Convert.ToString(canidateListBox.SelectedItem)

    voteWriter.Write(vote)
End Sub
4

1 回答 1

2

Make sure to close the stream after you are done with it.

voteWriter.Close()

Or you could use it in a using statement that will automatically close it at the end.

于 2015-04-16T00:58:16.360 回答