Reading a file that is already open by another process

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

I am writing an application that needs to read a file that is already open by another process for writing.

When I do the following:

FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);

I get the following exception:
{"The process cannot access the file \"EV1_0.log\" because it is being used by another process." }

I wrote a test program in "regular" C as follows and do not get this error. It can always read this file.

if ((fp = fopen("EV1_0.log", "r")) == NULL)
{
printf("Failed to open file");
return;
}

if (fgets (result,200, fp) == NULL)
{
printf("Could not read from file");
return;
}

printf(result);

The process that has the file open for writing is written in C and cannot be modified.

Does anyone know how I can access this file for reading using C#? I tried using FileShare.Read as one of the FileStream constructor values and that does not seem to help.

Thanks,
Trellow
 
trellow, try using this line to open your filestream:

FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read,
System.IO.FileShare.ReadWrite)

This overload of the FileStream constructor allows you to open the file in a
non-exclusive mode.

trellow said:
Hello,

I am writing an application that needs to read a file that is already open
by another process for writing.
When I do the following:

FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);

I get the following exception:
{"The process cannot access the file \"EV1_0.log\" because it is being used by another process." }

I wrote a test program in "regular" C as follows and do not get this
error. It can always read this file.
if ((fp = fopen("EV1_0.log", "r")) == NULL)
{
printf("Failed to open file");
return;
}

if (fgets (result,200, fp) == NULL)
{
printf("Could not read from file");
return;
}

printf(result);

The process that has the file open for writing is written in C and cannot be modified.

Does anyone know how I can access this file for reading using C#? I tried
using FileShare.Read as one of the FileStream constructor values and that
does not seem to help.
 
If the other process has specified that the file should not be shared, then
I think you're out of luck, but try with the FileShare.ReadWrite when
opening.

Arild

trellow said:
Hello,

I am writing an application that needs to read a file that is already open
by another process for writing.
When I do the following:

FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);

I get the following exception:
{"The process cannot access the file \"EV1_0.log\" because it is being used by another process." }

I wrote a test program in "regular" C as follows and do not get this
error. It can always read this file.
if ((fp = fopen("EV1_0.log", "r")) == NULL)
{
printf("Failed to open file");
return;
}

if (fgets (result,200, fp) == NULL)
{
printf("Could not read from file");
return;
}

printf(result);

The process that has the file open for writing is written in C and cannot be modified.

Does anyone know how I can access this file for reading using C#? I tried
using FileShare.Read as one of the FileStream constructor values and that
does not seem to help.
 
Back
Top