How do I read a text file that's in use by another process ?

  • Thread starter Thread starter BenSimmons
  • Start date Start date
B

BenSimmons

Hi i'm creating a program that reads various log files from another
program (Find-a-Drug DC project).
The problem is, when I read some of the logs I get the error
This process cannot access the file"C:\...." because it
is being used by another process

Well I know that :) as it's the log files for a constantly running DC
program.

The code below is what I'm using.

Public Shared Function ReadWholeFile(ByVal
FilePath As String)
Dim st As String
Try
' Create an instance of StreamReader to read from a file.
Dim sr As StreamReader = New
StreamReader(FilePath)
st = sr.ReadToEnd
sr.Close()
Catch E As Exception
' Let the user know what went wrong.
MessageBox.Show(E.Message.Trim, "The file could
not be read:")
End Try
Return (st)
End Function

The log files are plain text files by the way.
I know the code works when the program is not running.

Now how do I read the file when it is 'in use'
I know it must be posible as you can open it in notepad no problem and
a monitoring tool written in Delphi4 can read it.
How do I do it in VB.Net 2K3?

thanks (relatively new to VB.NET, I've mainly use VBA in Excel)
 
BenSimmons said:
Hi i'm creating a program that reads various log files from
another program (Find-a-Drug DC project).
The problem is, when I read some of the logs I get the error
This process cannot access the file"C:\...." because it
is being used by another process

Well I know that :) as it's the log files for a constantly running
DC program.

The code below is what I'm using.

Public Shared Function ReadWholeFile(ByVal
FilePath As String)
Dim st As String
Try
' Create an instance of StreamReader to read from a
file. Dim sr As StreamReader = New
StreamReader(FilePath)

Open a filestream instead. You can pass System.IO.FileAccess.Read to the
constructor. Then you can create a new streamreader to read from the stream.
If opening the file in read-only mode is also not possible, the other
process locks the file also for read operations, so you can not open it at
all.


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
The StreamReader constructor attempts to open the file with a default
FileShare value of Read but your other process already has the file open for
write access so the attempt to open fails. Instead of creating the
StreamReader directly, create a FileStream using a FileShare value of
ReadWrite and then create the StreamReader from that FileStream.

BenSimmons said:
Hi i'm creating a program that reads various log files from another
program (Find-a-Drug DC project).
The problem is, when I read some of the logs I get the error
This process cannot access the file"C:\...." because it
is being used by another process

Well I know that :) as it's the log files for a constantly running DC
program.

The code below is what I'm using.

Public Shared Function ReadWholeFile(ByVal
FilePath As String)
Dim st As String
Try
' Create an instance of StreamReader to read from a file.
Dim sr As StreamReader = New
StreamReader(FilePath)
st = sr.ReadToEnd
sr.Close()
Catch E As Exception
' Let the user know what went wrong.
MessageBox.Show(E.Message.Trim, "The file could
not be read:")
End Try
Return (st)
End Function

The log files are plain text files by the way.
I know the code works when the program is not running.

Now how do I read the file when it is 'in use'
I know it must be posible as you can open it in notepad no problem and
a monitoring tool written in Delphi4 can read it.
How do I do it in VB.Net 2K3?

thanks (relatively new to VB.NET, I've mainly use VBA in Excel)
 
Once I've figured out what you just told me i'll give it a go :)
off to read up on FileStream

It is possible to read it in Delphi4 language as there is a tool out
that parses the file. Just hoping I can do it in VB.NET (i'm making
a my own program to do it to learn :D )
 
Well I played around a bit to see what did what..

I ended up with this

Dim str As Stream = File.Open(FileName, FileMode.Open,
FileAccess.Read, FileShare.ReadWrite)
Dim sr As StreamReader
sr = New StreamReader(str)

and it works, of course the important part which I had forgotten you
had actually mentioned (ie forgotten for many hours playing around)
is the FileShare.ReadWrite part, without that the original error
would always come up.

Thankyou very much :D
 
Back
Top