I'm running a windows service in Windows 2003. I've been adding/changing code to it every day for the last month, and everyday for the last month my filewatcher has been working. However, it stopped working today for some odd reason. If I revert back to old code, it still won't raise events. I've tested the same code on my Win7 machine and it is working fine. I'm assuming there is an external process interfering but I'm not even sure what to look for.
Here is the relevant code:
private void InitializeComponent()
{
this.processfileWatcher = new System.IO.FileSystemWatcher();
((System.ComponentModel.ISupportInitialize) (this.processfileWatcher)).BeginInit();
this.processfileWatcher.EnableRaisingEvents = true;
this.processfileWatcher.Filter = "done.txt";
this.processfileWatcher.Changed += new System.IO.FileSystemEventHandler(this.processfileWatcher_Changed);
this.ServiceName = "Service1";
((System.ComponentModel.ISupportInitialize)(this.processfileWatcher)).EndInit();
}
private void processfileWatcher_Changed(object sender, System.IO.FileSystemEventArgs e)
{
try
{
processfileWatcher.EnableRaisingEvents = false;
//Do stuff here
Debug.WriteLine(" End of processfileWatcher for: " + e.FullPath);
}
finally
{
processfileWatcher.EnableRaisingEvents = true;
}
}
Through debug statements I can confirm that I am reaching the end of my onStart() method.
If something is causing a lot of rapid disk changes you may be running out of buffer before you have the chance to catch the thing you are looking for, or your service's credentials may not be working for what you are monitoring.
Try granting explicit permissions then look for the buffer issue by adding a handler for the fileystemwatcher's error event.
http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.error%28v=vs.100%29.aspx