PC Review


Reply
Thread Tools Rate Thread

Cannot READ a file being used by another process

 
 
Woody
Guest
Posts: n/a
 
      27th Oct 2005
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

 
Reply With Quote
 
 
 
 
Mehdi
Guest
Posts: n/a
 
      27th Oct 2005
On 27 Oct 2005 07:48:23 -0700, Woody wrote:

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


Instead of creating a StreamReader directly, have you tried to use the
overloaded version of File.Open() which takes a FileShare as a parameter,
specifying that you want to allow sharing read/write access to the file
(FileShare.ReadWrite)? Something like that:

FileStream stream = File.Open(filename, FileMode.Open, FileAccess.Read,
FileShare.ReadWrite);
 
Reply With Quote
 
Woody
Guest
Posts: n/a
 
      27th Oct 2005
It Works!
I would like to know why it fails if I use
FileStream stream = File.Open(filename, FileMode.Open, FileAccess.Read,
FileShare.Read)?

FileShare.Read instead of FileShare.Write

?

 
Reply With Quote
 
Mehdi
Guest
Posts: n/a
 
      27th Oct 2005
On 27 Oct 2005 10:01:40 -0700, Woody wrote:

> It Works!
> I would like to know why it fails if I use
> FileStream stream = File.Open(filename, FileMode.Open, FileAccess.Read,
> FileShare.Read)?
>
> FileShare.Read instead of FileShare.Write
>
> ?


Whith FileShare.Read, you are allowing other processes to open the file to
read it but not to write it. Since the Windows Service you were talking
about in your first post has the file open to write it, you need to allow
read/write access sharing if you want to be able to open it or it will fail
because another process already has the file open for writing.
 
Reply With Quote
 
Woody
Guest
Posts: n/a
 
      28th Oct 2005

Now it works and I also understand why.
Thanks a lot!

 
Reply With Quote
 
jeff.reimann@softhome.net
Guest
Posts: n/a
 
      2nd Nov 2005
Try opening thru a file stream rather then direct opening with a filename...

FileStream file = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

StreamReader roseFile = new StreamReader(file);

**********************************************************************
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot READ a file being used by another process fabrizio.viggiani@gmail.com Microsoft C# .NET 2 27th Oct 2005 04:13 PM
Read a file that is being used by another process. funkmusha Microsoft VB .NET 4 6th Sep 2005 10:49 PM
How to read a text file that is being used by another process? UnnamedIdiot Microsoft VB .NET 3 14th Jan 2005 03:02 AM
How to read a log file currently used by other process? Thomas Lin via .NET 247 Microsoft C# .NET 0 16th Apr 2004 08:01 PM
re:How do I read a text file that's in use by another process ? BenSimmons Microsoft VB .NET 1 9th Feb 2004 03:02 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:25 PM.