【发布时间】: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))
我在哪里调试并按下相应的按钮。谁能指出问题是什么?谢谢
【问题讨论】: