Read/Loop Textfile when it is used by another process

F

Frank Uray

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.

I am opening like:
System.IO.FileStream local_FileStream = new System.IO.FileStream("File.log",
System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);

How can I open a file when it is open ??
For example, I can open this file with Notepad ...

Thanks for any comments
Best regards
Frank
 
P

Pavel Minaev

Frank Uray said:
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.

I am opening like:
System.IO.FileStream local_FileStream = new
System.IO.FileStream("File.log",
System.IO.FileMode.Open, System.IO.FileAccess.Read,
System.IO.FileShare.Read);

The meaning of FileShare is not what you intend to do with the file, it's
what you want to allow other programs to do with the file while you keep it
open. So, FileShare.Read means that you only allow other programs to read
the file, and not write it. Since another application is already writing to
it, your request is denied. Try using FileShare.ReadWrite instead.
 
F

Frank Uray

Hi

Thanks a lot, this was it.

By the way, do you know how to read the lines
from the file beginning at the end of the file ??

Best regards
Frank
 
P

Pavel Minaev

Frank Uray said:
By the way, do you know how to read the lines
from the file beginning at the end of the file ??

Seek to the end of FileStream you've opened, and only then create a
StreamReader wrapping it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top