System.IO.FileSystemWatcher problem
-
Hello there, I need to monitor changes made to a certain file from ASP.NET website application. I decided to accomplish this using System.IO.FileSystemWatcher. Currently I am having the following problems 1. I can't make my watcher monitor a single file not just the whole folder content. 2. Even if I try the whole-folder-monitoring scenario it turns out that the Change event fires 5 times instead of 1. Any magic behind this number? Here's the source code inside my Global.asax file. Commented lines include adjustments I tried to enable monitoring specific file.
1 void Application_Start(object sender, EventArgs e) 2 { 3 // Code that runs on application startup 4 5 6 7 System.IO.FileSystemWatcher __file_watcher = new System.IO.FileSystemWatcher(); 8 Application.Add("AdConfigurationWatcher",__file_watcher); 9 __file_watcher.Path = Server.MapPath("~/"); 10 __file_watcher.IncludeSubdirectories = false; 11 __file_watcher.NotifyFilter = System.IO.NotifyFilters.LastWrite; 12 //__file_watcher.Filter = "myconfig.xml"; 13 14 15 __file_watcher.Changed += new System.IO.FileSystemEventHandler(__file_watcher_Changed); 16 17 __file_watcher.EnableRaisingEvents = true; 18 } 19 20 void __file_watcher_Changed(object sender, System.IO.FileSystemEventArgs e) 21 { 22 //do whatever.... this is just for tracing 23 System.IO.WatcherChangeTypes c = e.ChangeType; 24 } 25 26 void Application_End(object sender, EventArgs e) 27 { 28 // Code that runs on application shutdown 29 System.IO.FileSystemWatcher __file_watcher = Application["AdConfigurationWatcher"] as System.IO.FileSystemWatcher; 30 Application.Remove("AdConfigurationWatcher"); 31 __file_watcher.Dispose(); 32 }