By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,422 Members | 1,338 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,422 IT Pros & Developers. It's quick & easy.

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

P: n/a
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?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
Nov 21 '05 #2

P: n/a


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!

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #3

P: n/a
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

Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.