How to read a text file that is being used by another process?

U

UnnamedIdiot

There ought to be a simple way, but my searches are coming up void. . .

The situation is simple. I have a text log file which is open and being
written to by another application. I wish to monitor the file for
particular entries and respond programmaticaly. I cannot open the file
via streamreader, or I get the typical exception that the file is being
used by another process. (I know this.)

I can create a filesystemmonitor object that will tell me when changes
are made, and then copy the file, and read the copy, but that will be
very inefficient. There MUST be a better way; I can always open the
file with NotePad (though subsequent updates do not show up in the
editor), how is this done? I don't see a corresponding temp file being
created anywhere. . .

Is there a elegant method available?
 
C

Chris Hyde

You can try to open the file using a FieStream object, and passing
FileAccess.Read/FileShare.ReadWrite in one of the constructor overloads.
Something like this:

Dim myReader As StreamReader = new StreamReader(new FileStream(fileName,
FileMode.Open, FileAccess.Read, FileShare.ReadWrite))

If this still does not work, then the original process has opened the
file locked for read operations...you may well then be stuck.

HTH...

Chris
 
U

UnnamedIdiot

Thanks, Chris -- that did the trick. I had already tried filestream
with FileAccess.Read to no avail. The FileShare setting did the trick.

This is poorly documented, IMHO. . .

Without FileShare.ReadWrite, the operation won't open the file. But the
entry for the FileShare Enumeration specifies:

"Allows subsequent opening of the file for reading or writing. If this
flag is not specified, any request to open the file for writing or
reading (by this process or another process) will fail until the file is
closed. However, if this flag is specified additional permissions might
still be needed to access the file."

This doesn't make it obvious that the setting also allows the opening of
a file which was already opened for exclusive writing by another
process!

I hope this posting helps solve this simply problem for others.

PS: Embedding the FileStream instantiation inside the StreamReader
instantiation is a nice touch. I didn't realize you could wrap the
StreamReader around a FileStream, but it seems obvious in retrospect. I
was prepared to write my own parser for the FileStream to implement a my
own ReadLine if I got FileStream to work!
 
J

jonathan

Turns out that your post was very useful! I had what you might call a
mirror image of your situation. I too was monitoring a log file. In my
case, however, the application generating the log wasn't putting a lock
on the log file while writing. My VB program was locking it with
StreamReader, but the application creating the log didn't complain. It
just didn't write to the log while my program had it locked, so I was
missing events in the log. Anyway, thanks to you and Chris.

Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top