By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,247 Members | 1,223 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,247 IT Pros & Developers. It's quick & easy.

Sharing an open file between processes

P: n/a
So I have a log file that I'm writing with one C# program and want to open
and read it in another one. I open it via :

StreamWriter log = File.AppendText(logPath);
log.AutoFlush = true;

but when I try to read it with another application, for instance, like :
StreamReader log = new StreamReader(logPath);
String logContents = log.ReadToEnd();
log.Close();

it fails complaining about it already being open by another application. Is
it possible to share a file between two processes such that one can be
writing to it and another can read from it? I'm not too concerned about
race conditions (like what happens if the reader catches the writer in the
middle of writing a buffer).

Thanks,
Doug Wyatt
Nov 15 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
You can share a file by setting the proper Share flags.

In the read-app, use,

File.Open(logPath, FileMode.Append, FileAccess.Write, FileShare.Read);

In the write-app, use,

File.Open(logPath, FileMode.Open, FileAccess.Read, FileShare.Write);

vJ

"Doug Wyatt" <ne*********@starbak.net> wrote in message
news:eg**************@TK2MSFTNGP09.phx.gbl...
So I have a log file that I'm writing with one C# program and want to open
and read it in another one. I open it via :

StreamWriter log = File.AppendText(logPath);
log.AutoFlush = true;

but when I try to read it with another application, for instance, like :
StreamReader log = new StreamReader(logPath);
String logContents = log.ReadToEnd();
log.Close();

it fails complaining about it already being open by another application. Is it possible to share a file between two processes such that one can be
writing to it and another can read from it? I'm not too concerned about
race conditions (like what happens if the reader catches the writer in the
middle of writing a buffer).

Thanks,
Doug Wyatt

Nov 15 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.