Created
July 24, 2013 18:17
-
-
Save andresmoschini/6073066 to your computer and use it in GitHub Desktop.
Print last added lines to a file in the console
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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