Hi,
(VB.NET, .NET 3.5)
I wrote the following function to read some text from txt file. It was working fine but now it's not. It keeps giving me this error message
"IOException was unhandled" and
" The process cannot access the file 'F:\kh_matt\ch1.txt' because it is being used by another process."
The ch1.txt is not even opened or being used by any program at all. I tried to move ch1.txt to another location (Drive D) still I got the same message error but just different location it says The process cannot access the file 'D:\ch1.txt' because it is being used by another process."
Here's my code block :
Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 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
It always points to this code :
Dim MyStream As New StreamReader(Path.Combine(Application.StartupPath, filelocation))
Where I debug and press the respective button. Can anyone point out what the problem is ? Thanks
-
Make sure that you close your stream & streamreader once you've finished reading the file, even when an exception is being thrown.
Use a try/finally block, and close the stream / streamreader in the finally block.
-
It seems you open the file twice, which is probably what's causing your error:
reader = New StreamReader(New FileStream(filelocation, FileMode.Open)) ... Dim MyStream As New StreamReader(Path.Combine(Application.StartupPath, filelocation))
Are you sure that's what you intend to do? It looks like you can remove
MyStream
and usereader
instead. Also, you don't have to usePath.Combine
, sincefilelocation
is not relative.Angkor Wat : Yeah, you got that right. Now I commented out this code block : 'If System.IO.File.Exists(filelocation) = True Then ' reader = New StreamReader(New FileStream(filelocation, FileMode.Open)) 'Else ' MsgBox(filelocation, MsgBoxStyle.OkOnly) 'End If -
I think this is your problem:
If System.IO.File.Exists(filelocation) = True Then reader = New StreamReader(New FileStream(filelocation, FileMode.Open))
If the file exists it will open a StreamReader on it, then try and open another StreamReader on the same file, which will lock the file, causing this line:
Dim MyStream As New StreamReader(Path.Combine(Application.StartupPath, filelocation))
to fail.
Also, some pointers:
- consider using the System.IO.File.ReadAllText() method instead, much easier
- if you must use streams, wrap them in a using block to ensure they're freed correctly, for example:
`
Dim vArray() As String using (Dim MyStream As New StreamReader(Path.Combine(Application.StartupPath, filelocation)) { vArray = MyStream.ReadToEnd.Split(CChar("$")) }
(sorry if the above code isn't 100% correct, I don't write much VB.Net)
Kobi : Good call with the `ReadAllText`. Didn't know that one.Angkor Wat : @Ian Kemp . Thanks ! :DPaul Suart : +1 for the using -
Thanks all for the reply. It's my mistake. I forgot to comment out my code that I wrote for testing earlier. After commenting this code out it works like before.
'If System.IO.File.Exists(filelocation) = True Then ' reader = New StreamReader(New FileStream(filelocation, FileMode.Open)) 'Else ' MsgBox(filelocation, MsgBoxStyle.OkOnly) 'End If
Have a good day.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.