469,658 Members | 1,862 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

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(strTodaysLogFile, System.IO.FileMode.Append,
System.IO.FileAccess.Write, System.IO.FileShare.Read);
this.m_swLog = new StreamWriter(this.m_fsLog);
if(this.m_swLog != null) this.m_swLog.AutoFlush = true;
}

pubic WriteLogMessage()
{
if(this.m_swLog != null) this.m_swLog.Write("[" +
DateTime.Now.ToString("HH:mm:ss") + "] " + aMsg + Environment.NewLine);
}
</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.FileStream fs = new FileStream(this.m_strCurrentFileName,
System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
System.IO.StreamReader sr = new StreamReader(fs);

while(sr.Peek() > -1) this.DisplayLogText(sr.ReadLine() +
Environment.NewLine);

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 6435
cnu wrote:
this.m_fsLog = new FileStream(strTodaysLogFile, System.IO.FileMode.Append,
System.IO.FileAccess.Write, System.IO.FileShare.Read);
This part is OK. You are opening the file for writing
(FileAccess.Write) and you grant other processes the right to Read
(FileShare.Read).
System.IO.FileStream fs = new FileStream(this.m_strCurrentFileName,
System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
This is where the problem is. You open the file for reading
(FileAccess.Read) 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.ReadWrite 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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by a | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.