473,498 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
3 2633
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


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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
6945
by: Chuck Amadi | last post by:
Sorry to bovver you again (again) here's script. I still can't see why the get_payload() doesn't produce the plain text message body of an emails in the testwwws users mailbox. As you can see I...
0
1553
by: Majordomo | last post by:
-- >>>> --36742377 **** Command '--36742377' not recognized. >>>> Content-Type: text/plain; charset=us-ascii **** Command 'content-type:' not recognized. >>>> Content-Transfer-Encoding: 7bit...
2
2885
by: fabrizio.viggiani | last post by:
I try to open a file that is opened and used by win32service for logging purposes in c# The following code line StreamReader sr = new StreamReader(filename); gets an exception: The process...
10
2519
by: Tibby | last post by:
I need to read/write not only text files, but binary as well. It seems like on binary files, it doesn't right the last 10% of the file. -- Thanks --- Outgoing mail is certified Virus...
6
3897
by: BenSimmons | last post by:
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...
4
27083
by: funkmusha | last post by:
I am trying to read a log file using vb.net. I get an error stating "The process cannot access the file 'C:\test.log' because it is being used by another process." Below is a sample of what I am...
0
1257
by: John | last post by:
I need to create an app to read text files and upload the data into SQL. The text files are in different format so I want to run a method based on the first line of my text files, 1. One of my...
3
2198
by: =?Utf-8?B?RnJhbmsgVXJheQ==?= | last post by:
Hi all I am trying to loop a Textfile. This Textfile is located on a shared drive and it is used by another process. When I try to read it I get an exeption: The process cannot access the...
2
8790
by: soni2926 | last post by:
Hi, I wanted to make a page where I can send a request to another page, and read it's response back, can someone point me to an example? it's just a basic page i need to hit, i can hit it directly...
0
7004
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6890
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5464
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4915
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3095
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1423
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
292
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.