I've searched the archives here and tried things I've found but nothing is working. Here's my problem: My Windows Service has to read the contents (1 line) of txt files. A source file comes in, I read it and process. Then I read the next source file. Sporadically I get the following exception: "The process cannot access the file "D:\Source\inputfile.txt" because it is being used by another process." Here is the function where I am sporadically getting this message:
The first file seems to process ok. The second one seems to fail sometimes and is ok other times, etc. Can anyone tell me why I am getting that exception?
Thanks!
-Mike
Code:
Private Function GetTranIdFromFile(ByVal astrFullPath As String) As String
' astrFullPath is D:\Source\inputfile.txt (different file name each time)
Dim strTranId As String
Dim objStreamReader As StreamReader
Try
objStreamReader = New StreamReader(astrFullPath)
strTranId = objStreamReader.ReadLine().Trim()
objStreamReader.Close()
objStreamReader = Nothing
Catch ex As Exception
'sometimes I get the "being used by another user" error
End Try
Return strTranId
End Function
The first file seems to process ok. The second one seems to fail sometimes and is ok other times, etc. Can anyone tell me why I am getting that exception?
Thanks!
-Mike