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)?? 1 6770
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 completed executing. Just like when I run the Exe in the cmd
prompt. In my code I am opening two...
|
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);
this.m_swLog = new StreamWriter(this.m_fsLog);
|
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
|
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
the problem is not so much my sound card, but that I am making some
fundamentally wrong...
|
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 MSAccess database.
How do I write a little web page to see what account I am running under? I
think I...
| |
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/..
|
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
data into postgresql with something like a pipe between the two threads
might make sense, keeping...
|
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
|
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
|
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 usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |