473,561 Members | 3,118 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simultaneously Write to and Read from the same file

cnu
My program generates a log file for every event that happens in the program.
So, I open the file and keep it open till the end. This is how I open the
file for writing:

<CODE>
public CLogHandler()
{
this.m_fsLog = new FileStream(strT odaysLogFile, System.IO.FileM ode.Append,
System.IO.FileA ccess.Write, System.IO.FileS hare.Read);
this.m_swLog = new StreamWriter(th is.m_fsLog);
if(this.m_swLog != null) this.m_swLog.Au toFlush = true;
}

pubic WriteLogMessage ()
{
if(this.m_swLog != null) this.m_swLog.Wr ite("[" +
DateTime.Now.To String("HH:mm:s s") + "] " + aMsg + Environment.New Line);
}
</CODE>

One of the requirements is to display the current log file contents in a
child form. So, I open the log file for reading like this :

<CODE>
System.IO.FileS tream fs = new FileStream(this .m_strCurrentFi leName,
System.IO.FileM ode.Open, System.IO.FileA ccess.Read, System.IO.FileS hare.Read);
System.IO.Strea mReader sr = new StreamReader(fs );

while(sr.Peek() > -1) this.DisplayLog Text(sr.ReadLin e() +
Environment.New Line);

fs.Close();
</CODE>

As you see, when I open the file for writing, I'm granting FileShare.Read,
which means other processes can open the file for reading. But when I try to
open the file for reading, it gives IOException that the file is being used
by another process.

So, how can we write and read to and from a file simultaneously in the same
program (ofcourse, without stopping the write process)??

Oct 4 '05 #1
1 6728
cnu wrote:
this.m_fsLog = new FileStream(strT odaysLogFile, System.IO.FileM ode.Append,
System.IO.FileA ccess.Write, System.IO.FileS hare.Read);
This part is OK. You are opening the file for writing
(FileAccess.Wri te) and you grant other processes the right to Read
(FileShare.Read ).
System.IO.FileS tream fs = new FileStream(this .m_strCurrentFi leName,
System.IO.FileM ode.Open, System.IO.FileA ccess.Read, System.IO.FileS hare.Read);
This is where the problem is. You open the file for reading
(FileAccess.Rea d) but then you try to restrict other processes by
saying they can only Read (FileShare.Read ). FileShare.Read means:
"Only let other processes Read from this file". Since you have already
opened the file for writing elsewhere, this is causing a conflict. You
should specify FileShare.ReadW rite when opening the file for reading.

As you see, when I open the file for writing, I'm granting FileShare.Read,
which means other processes can open the file for reading. But when I try to


Right, but when you open it for reading, you specified FileShare.Read
which means other processes cannot write. Since your first process is
already writing, it can't open in this mode.

Oct 4 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1483
by: BasicQ | last post by:
I am running an executable from my aspx page with the click of a button. A date is passed as an argument. I am able to get the standardoutput from the Process(Exe) into the label of my page after the process has completed executing. My problem is I need to get the output in the label simultaneously when the EXE is running, not after it has...
1
303
by: cnu | last post by:
My program generates a log file for every event that happens in the program. So, I open the file and keep it open till the end. This is how I open the file for writing: <CODE> public CLogHandler() { this.m_fsLog = new FileStream(strTodaysLogFile, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.Read);...
8
23894
by: a | last post by:
I have a struct to write to a file struct _structA{ long x; int y; float z; } struct _structA A; //file open write(fd,A,sizeof(_structA)); //file close
1
2528
by: Tito | last post by:
For an internet telephone application, I need to be able to read and write data to and from /dev/dsp simultaneously. I wrote some code and its not working. Anyone have any working code to do this? I am assuming my card is full duplex, it is a built-in sound card on a new dell 600m laptop, but I am not sure how to tell for sure. But I think...
1
1835
by: Siegfried Heintze | last post by:
I have some cygwin cron jobs running under the Administrator account populating a MSAccess database that is simultaneously being queried by IIS/ASP.NET/C#. If I manually use windows explorer to allow everyone to read/write/delete the .mdl files created by the cygwin cron jobs, everything works. If I don't, IIS/ASP.NET cannot open the...
8
10493
by: Raghu | last post by:
Is it possible to write a new file and after writing few bytes, is it possible to read from it from another file stream while write continues? Is there another steam for this type of operation? Thanks. Raghu/..
12
5451
by: Sean Davis | last post by:
I am working on a simple script to read from one database (oracle) and write to another (postgresql). I retrieve the data from oracle in chunks and drop the data to postgresql continuously. The author of one of the python database clients mentioned that using one thread to retrieve the data from the oracle database and another to insert the...
0
789
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted by the software in your host machine. Stack Trace at System.Net.Sockets.NetworkStream.Write(Byte buffer, Int32 offset, Int32
3
14037
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted by the software in your host machine. Stack Trace at System.Net.Sockets.NetworkStream.Write(Byte buffer, Int32 offset, Int32
0
7637
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
7851
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8072
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...
1
7605
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7924
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...
1
5462
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
3609
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3591
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1171
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.