473,769 Members | 8,305 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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(B yVal
FilePath As String)
Dim st As String
Try
' Create an instance of StreamReader to read from a file.
Dim sr As StreamReader = New
StreamReader(Fi lePath)
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)
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 20 '05 #1
6 3944
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 " <vb@simmo.f9. co-dot-uk.no-spam.invalid> wrote in message
news:40******** **@Usenet.com.. .
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(B yVal
FilePath As String)
Dim st As String
Try
' Create an instance of StreamReader to read from a file.
Dim sr As StreamReader = New
StreamReader(Fi lePath)
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)
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com

Nov 20 '05 #2
"BenSimmons " <vb@simmo.f9. co-dot-uk.no-spam.invalid> schrieb
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(B yVal
FilePath As String)
Dim st As String
Try
' Create an instance of StreamReader to read from a
file. Dim sr As StreamReader = New
StreamReader(Fi lePath)


Open a filestream instead. You can pass System.IO.FileA ccess.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

Nov 20 '05 #3
Cor
Hi Armin,

Did not know this, but I thougth it,

Thanks,

Cor
Open a filestream instead. You can pass System.IO.FileA ccess.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.

Nov 20 '05 #4
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 )
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 20 '05 #5
Well I played around a bit to see what did what..

I ended up with this

Dim str As Stream = File.Open(FileN ame, FileMode.Open,
FileAccess.Read , FileShare.ReadW rite)
Dim sr As StreamReader
sr = New StreamReader(st r)

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.ReadW rite part, without that the original error
would always come up.

Thankyou very much :D
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 20 '05 #6
Cor
Hi Bill,

Thanks for your feed back to this newsgroup.

Cor
Well I played around a bit to see what did what..

I ended up with this

Dim str As Stream = File.Open(FileN ame, FileMode.Open,
FileAccess.Read , FileShare.ReadW rite)
Dim sr As StreamReader
sr = New StreamReader(st r)

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.ReadW rite part, without that the original error
would always come up.

Thankyou very much :D

Nov 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
6989
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
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.
7
960
by: JerryW | last post by:
I just reinstalled .NET 2003 (after repeated attempts to get ASP.NET Web Applications to work). I first did a complete uninstall of .NET 2003, .NET Framework 1.1, and IIS. I also completely deleted c:\inetpub\wwwroot and rebooted before reinstalling. I created a new virtual directory from within IIS (e.g. MyWebTest). Then I tried to create a new Visual C# ASP.NET Web Application pointing to the location http://localhost/MyWebTest. I get...
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
3
2653
by: UnnamedIdiot | last post by:
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...
4
27156
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
2233
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.
4
4753
by: Bruno | last post by:
Hi! I have big .txt file which i want to read, process and write to another .txt file. I have done script for that, but im having problem with croatian characters (Š,Đ,Ž,Č,Ć). How can I read/write from/to file in utf-8 encoding? I read file with fileinput.input. thanks
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
10216
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...
1
7413
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
6675
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
5310
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
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3965
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 we have to send another system
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
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.