Skip to content

Instantly share code, notes, and snippets.

@andresmoschini
Created July 24, 2013 18:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andresmoschini/6073066 to your computer and use it in GitHub Desktop.
Save andresmoschini/6073066 to your computer and use it in GitHub Desktop.
Print last added lines to a file in the console
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
namespace ConsoleApplication7
{
class Program
{
static StreamReader streamReader;
static void Main(string[] args)
{
var fileStream = File.Open("C:\\Bandeja\\test.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
streamReader = new StreamReader(fileStream);
var watcher = new System.IO.FileSystemWatcher("C:\\Bandeja", "test.txt");
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
watcher.EnableRaisingEvents = true;
watcher.Changed += (object sender, FileSystemEventArgs e) => {
ReadAndWrite();
};
ReadAndWrite();
while (Console.Read() != 'q') ;
}
static void ReadAndWrite()
{
Console.WriteLine(streamReader.ReadToEnd());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment