Mega Code Archive

 
Categories / C# / File Stream
 

Set NotifyFilter of FileSystemWatcher

using System; using System.IO; public class Program {     static void Main(string[] args) {         FileSystemWatcher watcher = new FileSystemWatcher();         try {             watcher.Path = @"C:\MyFolder";         } catch (ArgumentException ex) {             Console.WriteLine(ex.Message);             return;         }         watcher.NotifyFilter = NotifyFilters.LastAccess             | NotifyFilters.LastWrite             | NotifyFilters.FileName             | NotifyFilters.DirectoryName;         watcher.Filter = "*.txt";         watcher.Changed += new FileSystemEventHandler(OnChanged);         watcher.Created += new FileSystemEventHandler(OnChanged);         watcher.Deleted += new FileSystemEventHandler(OnChanged);         watcher.Renamed += new RenamedEventHandler(OnRenamed);         watcher.EnableRaisingEvents = true;         Console.WriteLine(@"Press 'q' to quit app.");         while (Console.Read() != 'q') ;     }     private static void OnChanged(object source, FileSystemEventArgs e) {         Console.WriteLine("File: {0} {1}!", e.FullPath, e.ChangeType);     }     private static void OnRenamed(object source, RenamedEventArgs e) {         Console.WriteLine("File: {0} renamed to\n{1}", e.OldFullPath, e.FullPath);     } }