Cannot READ a file being used by another process

F

fabrizio.viggiani

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.

I can open the file using notepad or I can use C++ using the following
code:
FILE *f = fopen(filename, "r");

Can someone tell me what I'm doing wrong.

Thanks
 
D

Dica

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.

you're probably opening a stream on the same file elsewhere or in a loop and
not closing it. check your code for any reference to the file and make sure
you close the streamReader before attempting to open the file again.
 
C

Carlos J. Quintero [VB MVP]

If you try to open it with a text editor or with a function that opens for
read/write (the default), it will fail if other process is doing the same.

You can use a FileStream instead and use the overloaded constructor that
uses a FileShare parameter and pass it FileShare.Read.

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com
 

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