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

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

P: n/a
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(ByVal
FilePath As String)
Dim st As String
Try
' Create an instance of StreamReader to read from a file.
Dim sr As StreamReader = New
StreamReader(FilePath)
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
Share this Question
Share on Google+
6 Replies


P: n/a
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(ByVal
FilePath As String)
Dim st As String
Try
' Create an instance of StreamReader to read from a file.
Dim sr As StreamReader = New
StreamReader(FilePath)
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

P: n/a
"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(ByVal
FilePath As String)
Dim st As String
Try
' Create an instance of StreamReader to read from a
file. Dim sr As StreamReader = New
StreamReader(FilePath)


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

P: n/a
Cor
Hi Armin,

Did not know this, but I thougth it,

Thanks,

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

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

P: n/a
Well I played around a bit to see what did what..

I ended up with this

Dim str As Stream = File.Open(FileName, FileMode.Open,
FileAccess.Read, FileShare.ReadWrite)
Dim sr As StreamReader
sr = New StreamReader(str)

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.ReadWrite 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

P: n/a
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(FileName, FileMode.Open,
FileAccess.Read, FileShare.ReadWrite)
Dim sr As StreamReader
sr = New StreamReader(str)

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

Thankyou very much :D

Nov 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.