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

Cannot READ a file being used by another process

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

Nov 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

<fa***************@gmail.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...
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.

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

Nov 17 '05 #2

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

<fa***************@gmail.com> escribió en el mensaje
news:11*********************@g43g2000cwa.googlegro ups.com...
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

Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.