Need shared access to file in C and C#

C

clintp

In C, I'm creating a log file like this:

upd = _fsopen(outputFile, "wt", _SH_DENYNO));

// writing... Here's a sample.
fprintf(upd, "Sample contents");

fclose(upd);


In another thread -- in the same process -- I'm attempting to open this
file with:

FileStream fs = new FileStream(convertFile, FileMode.Open,
FileAccess.Read, FileShare.Read);

Which is throwing:

The process cannot access the file '[your file here]' because it is
being used by another process.
A first chance exception of type 'System.IO.IOException' occurred in
mscorlib.dll

As far as I can tell, I'm doing everything right. In the C code, I'm
leaving the file as open for sharing as I possibly can, and in the C#
I'm opening it with the right flags.
 
W

Willy Denoyette [MVP]

In C, I'm creating a log file like this:

upd = _fsopen(outputFile, "wt", _SH_DENYNO));

// writing... Here's a sample.
fprintf(upd, "Sample contents");

fclose(upd);


In another thread -- in the same process -- I'm attempting to open this
file with:

FileStream fs = new FileStream(convertFile, FileMode.Open,
FileAccess.Read, FileShare.Read);

Which is throwing:

The process cannot access the file '[your file here]' because it is
being used by another process.
A first chance exception of type 'System.IO.IOException' occurred in
mscorlib.dll

As far as I can tell, I'm doing everything right. In the C code, I'm
leaving the file as open for sharing as I possibly can, and in the C#
I'm opening it with the right flags.


No, you don't. You are trying to open the file in shared read mode (FileShare.Read), which
isn't possible as the file is already open in shared read/write mode, so, you need to share
the file for read/write too.

Willy.
 
C

clintp

FileStream fs = new FileStream(convertFile, FileMode.Open,
FileAccess.Read, FileShare.Read);

As far as I can tell, I'm doing everything right. In the C code, I'm
leaving the file as open for sharing as I possibly can, and in the C#
I'm opening it with the right flags.

Following up on my own post. The problem is, of course, that the:

FileShare.Read

Should have been:

FileShare.ReadWrite


...

D'oh.
 

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