Cannot READ a file being used by another process

  • Thread starter Thread starter fabrizio.viggiani
  • Start date Start date
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
 
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.
 
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
 
Back
Top