473,757 Members | 10,263 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 filesystemmonit or 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 2651
You can try to open the file using a FieStream object, and passing
FileAccess.Read/FileShare.ReadW rite in one of the constructor overloads.
Something like this:

Dim myReader As StreamReader = new StreamReader(ne w FileStream(file Name,
FileMode.Open, FileAccess.Read , FileShare.ReadW rite))

If this still does not work, then the original process has opened the
file locked for read operations...yo u 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.ReadW rite, 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
6987
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 have tried a few things but no joy what am I missing. Is the another snippet in relation to get_payload to access the body contents print and process to a file. Cheers
0
1568
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 **** Command 'content-transfer-encoding:' not recognized. >>>> >>>> something is fool
2
2898
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 cannot access the file 'XX' because it is being used by another process.
10
2545
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 Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.538 / Virus Database: 333 - Release Date: 11/10/2003
6
3944
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 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.
4
27153
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 trying to do. The var logFileName is being passed into the function. Dim line as String Dim sr As New StreamReader(logFileName) 'It dies on this line Do line = sr.ReadLine()
0
1279
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 text files look like this: New Sales BMW, VIN12345678989, 12122005,0,JSmith Lexus, VIN98765432100, 01012004,1,JSmith 2. another text file looks like this;
3
2232
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 file 'file.log' because it is being used by another process.
2
8803
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 with my browser, no user/pass, but i want to read whats returned into a webpage, it's going to come back as html, wasn't sure if there's a control to just display whats returned, otherwise i can parse it, but main thing was how to send a request...
0
9298
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10072
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9906
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8737
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7286
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6562
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5172
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2698
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.