simultaneous file access

C

Chris

hi, i'm having trouble with file access/permssions.
is it possible for me to have one app writing to a file and another app
reading from the same file?
i'm writing a log reader that hopefully will read a log file and output
whatever is appended to the log in real time.


to demonstrate my problem, i wrote a couple apps.
run app1 first and it will block after the file is opened, so you can start
app2 and see the problem i'm having.

make sure they're both in the same directory.

thanks,
chris

// app1.cs:
using System;
using System.IO;

class App1
{
static void Main()
{
FileStream fs = new FileStream("trace.log", FileMode.Append,
FileAccess.Write, FileShare.Read);
Console.WriteLine("now start app2");
Console.Read(); // block so app2 can be run
fs.Close();
}
}

// app2.cs:
using System;
using System.IO;

class App2
{
static void Main()
{
try {
FileStream fs = new FileStream("trace.log", FileMode.Open,
FileAccess.Read);
Console.WriteLine("successful");
} catch(Exception ex) {
Console.WriteLine(ex.ToString());
}
Console.WriteLine("any key to close");
Console.Read(); // block
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top