473,414 Members | 1,690 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

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 34450
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Pankaj | last post by:
Hi All: Is there a solution to below problem...without....restarting VS.Net 2003 so may be sometimes restarting PC. // Cannot copy assembly 'MyClient.Client' to file...
0
by: Keith | last post by:
Hi. I've developed an ASP.NET web service that uses an xml file to store configuration settings. I'm creating a cache dependency based on the xml file and its schema, like this... XmlDocument...
3
by: UnnamedIdiot | last post by:
There ought to be a simple way, but my searches are coming up void. . . The situation is simple. I have a text log file which is open and being written to by another application. I wish to...
2
by: Guy_B | last post by:
Hi I am using the filesystem watcher to pick up files as they are dropped into a folder - the files are output file from another system and I have no control over that. When 'watcher raises the...
13
by: George | last post by:
Hi, I am re-writing part of my application using C#. This application starts another process which execute a "legacy" program. This legacy program writes to a log file and before it ends, it...
2
by: samar | last post by:
I have a C++ programme in MPI-environment which reads its Input from a file previously made by another C++ programme , but now I face a problem , the new programme can't read that data from the...
2
by: =?Utf-8?B?Z2Fsc2k=?= | last post by:
Hi I am trying to read a file or get it's size while this file is written by another process. When i use the windows explorer i can read the file or check it's size . When i use the...
5
by: heday60 | last post by:
I've got this application that watches a directory for XML's and then parses them into a DB. It then moves them based on if the XML was succesful or a bad XML... foreach(FileInfo file in...
1
by: Hossam Tebry | last post by:
I use filesystemwatcher in the created event i go read the file sequentially do some checks and then close the straem reader then i move the file chang the name and the directory. private void...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.