【问题标题】:Strange "IOException was unhandled"奇怪的“IOException 未处理”
【发布时间】:2010-10-12 15:15:23
【问题描述】:

(VB.NET,.NET 3.5)

我编写了以下函数来从 txt 文件中读取一些文本。它工作正常,但现在不行了。它一直给我这个错误信息

“IOException 未处理”和

" 进程无法访问文件 'F:\kh_matt\ch1.txt',因为它正被另一个进程使用。"

ch1.txt 甚至没有被任何程序打开或使用。我试图将 ch1.txt 移动到另一个位置(驱动器 D),但仍然收到相同的消息错误,但只是显示不同的位置进程无法访问文件“D:\ch1.txt”,因为它正被另一个进程使用。 "

这是我的代码块:

Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 处理 btnRead.Click

    Dim reader As StreamReader
    Dim filelocation As String

    filelocation = "F:\kh_matt\ch1.txt"
    Dim chid As Integer

    chid = 1


    If System.IO.File.Exists(filelocation) = True Then
        reader = New StreamReader(New FileStream(filelocation, FileMode.Open))
    Else
        MsgBox(filelocation, MsgBoxStyle.OkOnly)
    End If

    Dim MyStream As New StreamReader(Path.Combine(Application.StartupPath, filelocation))
    Dim vArray() As String = MyStream.ReadToEnd.Split(CChar("$"))
    MyStream.Close()


    Dim count As Integer

    For d As Integer = 0 To vArray.Length - 1 Step 1

        If d = vArray.Length - 1 Then
            Exit For
        End If

        InsertKh(chid, d + 1, vArray(d))
        count = d + 1
    Next


    MsgBox("Done Inserting")
End Sub

它总是指向这个代码:

将 MyStream 调暗为新 StreamReader(Path.Combine(Application.StartupPath, filelocation))

我在哪里调试并按下相应的按钮。谁能指出问题是什么?谢谢

【问题讨论】:

    标签: vb.net exception


    【解决方案1】:

    我认为这是你的问题:

    If System.IO.File.Exists(filelocation) = True Then
        reader = New StreamReader(New FileStream(filelocation, FileMode.Open))
    

    如果文件存在,它将在其上打开一个 StreamReader,然后尝试在同一文件上打开另一个 StreamReader,这将锁定该文件,从而导致此行:

    Dim MyStream As New StreamReader(Path.Combine(Application.StartupPath, filelocation))
    

    失败。

    还有一些提示:

    • 考虑改用 System.IO.File.ReadAllText() 方法,会容易得多
    • 如果您必须使用流,请将它们包装在 using 块中以确保它们被正确释放,例如:

    `

    Dim vArray() As String
    
    using (Dim MyStream As New StreamReader(Path.Combine(Application.StartupPath, filelocation))
    {
      vArray = MyStream.ReadToEnd.Split(CChar("$"))
    }
    

    (对不起,如果上面的代码不是 100% 正确,我不会写太多 VB.Net)

    【讨论】:

    • ReadAllText 良好通话。不知道那个。
    【解决方案2】:

    您似乎打开了两次文件,这可能是导致您的错误的原因:

    reader = New StreamReader(New FileStream(filelocation, FileMode.Open))
    ...
    Dim MyStream As New StreamReader(Path.Combine(Application.StartupPath, filelocation))
    

    你确定这是你打算做的吗?看起来您可以删除 MyStream 并改用 reader。此外,您不必使用Path.Combine,因为filelocation 不是相对的。

    【讨论】:

    • 是的,你没看错。现在我注释掉了这个代码块: 'If System.IO.File.Exists(filelocation) = True Then ' reader = New StreamReader(New FileStream(filelocation, FileMode.Open)) 'Else ' MsgBox(filelocation, MsgBoxStyle.OkOnly) '结束如果
    【解决方案3】:

    确保在完成读取文件后关闭流和流读取器,即使抛出异常也是如此。

    使用 try/finally 块,并在 finally 块中关闭流/streamreader。

    【讨论】:

      【解决方案4】:

      感谢大家的回复。这是我的错。我忘记注释掉我之前为测试而编写的代码。注释掉这段代码后,它就像以前一样工作。

          'If System.IO.File.Exists(filelocation) = True Then
          '    reader = New StreamReader(New FileStream(filelocation, FileMode.Open))
          'Else
          '    MsgBox(filelocation, MsgBoxStyle.OkOnly)
          'End If
      

      祝你有美好的一天。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-08-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多