My goal is to read the last inserted lines from a txt file and show only newly inserted lines in the console. Here is my code-
class Program
{
static void Main(string[] args)
{
string path = @"D:\tmp";
MonitorFile(path);
Console.ReadKey();
}
private static void MonitorFile(string path)
{
FileSystemWatcher fileSystemWatcher = new FileSystemWatcher();
fileSystemWatcher.Path = path;
fileSystemWatcher.Filter = "file.txt";
fileSystemWatcher.Changed += FileSystemWatcher_Changed;
fileSystemWatcher.EnableRaisingEvents = true;
}
private static void FileSystemWatcher_Changed(object sender, FileSystemEventArgs e)
{
Console.WriteLine(""); //From here new inserted lines should be showed on the console.
}
}
My target is that each time new lines will be added at the last of the text file and only inserted new lines will come one after another in console. Here I used FileSystemWatcher for identify the change in the txt file but it seems like inserted data can't be read from FileSystemWatcher. Any help will be really appreciable.