FileSystemWatcher
This class
monitors a directory for changes. A program may need to process new files (often written to the directory by other programs).
FileSystemWatcher
provides a way to monitor a directory for file changes, creations, deletions and renames. We do not need Windows Forms to use it.
This program uses a FileSystemWatcher
static
field. We initialize it with the new keyword in Init()
. We add its Changed event handler.
bool
to indicate what happened—so we just set "needs update" to true.using System; using System.IO; class Program { static void Main() { Console.WriteLine("MAIN"); Init(); // Run an infinite loop. while (true) { Console.WriteLine("TYPE SOMETHING"); string line = Console.ReadLine(); Console.WriteLine("TYPED: " + line); } } /// <summary> /// Watcher. /// </summary> static FileSystemWatcher _watcher; /// <summary> /// Init. /// </summary> static void Init() { Console.WriteLine("INIT"); string directory = @"C:\programs\"; Program._watcher = new FileSystemWatcher(directory); Program._watcher.Changed += new FileSystemEventHandler(Program._watcher_Changed); Program._watcher.EnableRaisingEvents = true; Program._watcher.IncludeSubdirectories = true; } /// <summary> /// Handler. /// </summary> static void _watcher_Changed(object sender, FileSystemEventArgs e) { Console.WriteLine("CHANGED, NAME: " + e.Name); Console.WriteLine("CHANGED, FULLPATH: " + e.FullPath); // Can change program state (set invalid state) in this method. // ... Better to use insensitive compares for file names. } }MAIN INIT TYPE SOMETHING test TYPED: test TYPE SOMETHING CHANGED, NAME: file.txt CHANGED, FULLPATH: C:\programs\file.txt CHANGED, NAME: file.txt CHANGED, FULLPATH: C:\programs\file.txt hello TYPED: hello TYPE SOMETHING CHANGED, NAME: file.txt CHANGED, FULLPATH: C:\programs\file.txt CHANGED, NAME: file.txt CHANGED, FULLPATH: C:\programs\file.txtHELLO WORLD! X2 X3
Before you can write any code, you must set up your Windows Forms program properly. Open the Toolbox and double-click on the FileSystemWatcher
icon.
fileSystemWatcher1
label. You can specify the directory you want to monitor for changes.using System.IO; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } void fileSystemWatcher1_Changed(object sender, FileSystemEventArgs e) { // Occurs when the contents of the file change. MessageBox.Show(string.Format("Changed: {0} {1}", e.FullPath, e.ChangeType)); } void fileSystemWatcher1_Created(object sender, FileSystemEventArgs e) { // FullPath is the new file's path. MessageBox.Show(string.Format("Created: {0} {1}", e.FullPath, e.ChangeType)); } void fileSystemWatcher1_Deleted(object sender, FileSystemEventArgs e) { // FullPath is the location of where the file used to be. MessageBox.Show(string.Format("Deleted: {0} {1}", e.FullPath, e.ChangeType)); } void fileSystemWatcher1_Renamed(object sender, RenamedEventArgs e) { // FullPath is the new file name. MessageBox.Show(string.Format("Renamed: {0} {1}", e.FullPath, e.ChangeType)); } } }
You may have a problem with receiving multiple Changed events for a single file change. This must be due to a technical detail about how some programs write files.
Timer
One of my favorite techniques for monitoring a program for changes is using a Timer
instance and simply doing a scan on a regular interval.
FileInfo
type as well as the Timer
type for this.We examined the FileSystemWatcher
class
. This type presents an excellent way to persistently monitor a directory for changes, without lots of custom code.