WriteFile:
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
namespace WriteFile
{
class Program
{
static void Main(string[] args)
{
Thread thread = new Thread(delegate()
{
using (FileStream fs = new FileStream("file.txt",
FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite))
using (StreamWriter sw = new StreamWriter(fs))
{
while (true)
{
try
{
fs.Position = 0;
sw.WriteLine(DateTime.Now.ToString());
sw.Flush();
}
finally
{
Thread.Sleep(500);
}
}
}
});
thread.IsBackground = true;
thread.Start();
Console.WriteLine("Press ENTER to exit");
Console.ReadLine();
}
}
}
ReadFile:
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
namespace ReadFile
{
class Program
{
static void Main(string[] args)
{
Thread thread = new Thread(delegate()
{
using (FileStream fs = new FileStream("file.txt",
FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite))
using (StreamReader sw = new StreamReader(fs))
{
while (true)
{
try
{
fs.Position = 0;
Console.WriteLine(sw.ReadLine());
}
finally
{
Thread.Sleep(500);
}
}
}
});
thread.IsBackground = true;
thread.Start();
Console.WriteLine("Press ENTER to exit");
Console.ReadLine();
}
}
}
FileSharing.zip