Is it possible to create 2 applications that one always writes to file (like a log file) and other application that can read from a file?
I'd show you what I've tried:
Writter application
static void Main(string[] args)
{
string file = @"D:\filetest.txt";
int i = 0;
while (true)
{
using(FileStream fs = File.OpenWrite(file))
{
using(StreamWriter sw = new StreamWriter(fs))
{
sw.WriteLine($"line line {i++}");
}
}
Thread.Sleep(500);
}
}
reader application
static void Main(string[] args)
{
string file = @"D:\filetest.txt";
int i = 0;
while (true)
{
string lines = string.Empty;
using (FileStream fs = File.OpenRead(file))
{
using (StreamReader sw = new StreamReader(fs))
{
lines = sw.ReadToEnd();
}
}
Console.WriteLine(lines);
Thread.Sleep(200);
}
}
Of course as I expected I got exception on File.OpenWrite(file)
line
"he process cannot access the file 'D:\filetest.txt' because it is being used by another process."
The only solution I think is to copy the written file before reading it.