468,121 Members | 1,523 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,121 developers. It's quick & easy.

Reading a file that is already open by another process

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
Nov 15 '05 #1
3 33127
Try specifying System.IO.FileShare.ReadWrite

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 15 '05 #2
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" <an*******@discussions.microsoft.com> wrote in message
news:6C**********************************@microsof t.com...
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

Nov 15 '05 #3
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" <an*******@discussions.microsoft.com> wrote in message
news:6C**********************************@microsof t.com...
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

Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Pankaj | last post: by
2 posts views Thread by =?Utf-8?B?Z2Fsc2k=?= | last post: by
5 posts views Thread by heday60 | last post: by
18 posts views Thread by didacticone | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.