【问题标题】:Unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dllmscorlib.dll 中出现未处理的“System.IO.IOException”类型异常
【发布时间】:2015-04-15 23:58:23
【问题描述】:

我在处理这段代码时遇到了问题。 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

【问题讨论】:

    标签: runtime-error vb.net-2010 streamwriter unhandled


    【解决方案1】:

    确保在完成后关闭流。

    voteWriter.Close()
    

    或者您可以在 using 语句中使用它,该语句将在最后自动关闭它。

    【讨论】:

    • 这似乎是正确的答案。关于使用该文件的其他程序有很多答案,但在大多数情况下,它只是一个未关闭的流。
    猜你喜欢
    • 1970-01-01
    • 2014-03-09
    • 1970-01-01
    • 2015-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多