我认为您必须跟踪您在文件中读取的最后一个位置,然后当您检测到更改时:打开文件,寻找正确的位置,然后读到最后。然后将其解析成行以添加到文本框中。
编辑: 这是一个演示这一点的工作控制台应用程序。你会想要更多的错误检查、初始化等等。旧代码只是一个猜测,但基本上是正确的。
class Program
{
static FileSystemWatcher fs = null;
static string fileName = @"c:\temp\log.txt";
static long oldPosition = 0;
static void Main(string[] args)
{
fs = new FileSystemWatcher(Path.GetDirectoryName(fileName));
fs.Changed += new FileSystemEventHandler(fs_Changed);
fs.EnableRaisingEvents = true;
Console.WriteLine("Waiting for changes to " + fileName);
Console.ReadLine();
}
static void fs_Changed(object sender, FileSystemEventArgs e)
{
if (e.FullPath != fileName || e.ChangeType != WatcherChangeTypes.Changed) return;
using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (StreamReader fr = new StreamReader(fs))
{
Console.WriteLine("{0} changed. Old Postion = {1}, New Length = {2}", e.Name, oldPosition, fs.Length);
if (fs.Length > oldPosition)
{
fs.Position = oldPosition;
var newData = fr.ReadToEnd();
Console.WriteLine("~~~~~~ new data ~~~~~~\n" + newData);
oldPosition = fs.Position;
}
}
}
}