473,564 Members | 2,758 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(file Name, 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.lo g", "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 34494
Try specifying System.IO.FileS hare.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(file Name, FileMode.Open, FileAccess.Read ,
System.IO.FileS hare.ReadWrite)

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

"trellow" <an*******@disc ussions.microso ft.com> wrote in message
news:6C******** *************** ***********@mic rosoft.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(file Name, 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.lo g", "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.ReadW rite when
opening.

Arild

"trellow" <an*******@disc ussions.microso ft.com> wrote in message
news:6C******** *************** ***********@mic rosoft.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(file Name, 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.lo g", "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
5170
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 'C:\bin\Debug\MyClient.Client.dll'. The process cannot access the file because it is being used by another process. -Pankaj
0
1621
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 doc = new XmlDocument(); { ........ } string filePaths = { filePath, schemaPath }; CacheDependency dep = new CacheDependency(filePaths);
3
2639
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 monitor the file for particular entries and respond programmaticaly. I cannot open the file via streamreader, or I get the typical exception that the...
2
2792
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 event that a new file has appeared then sometimes I can read the file even tho it is still being written but it is incomplete. Other time a file...
13
11119
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 writes a specific string to the log file. My original program (MKS Toolkit shell program) which keeps running "grep" checking the "exit string" on...
2
1331
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 existing file , I think since these 2 programmes are compiled with 2 different compilers , the problem could come from that point , so whether my idea is...
2
1468
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 FileInfo(size) it hangs until file is released. Is there another way? Thanks
5
3176
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 dir.GetFiles("*.xml")) { try { ParseXML(file.FullName); }
1
1831
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 WABfileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e) { foreach (FileInfo INF in WABdir.GetFiles("*.spl")) ...
0
7665
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7583
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8106
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7950
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6255
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2082
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.