Search code examples
c#visual-studio-2010visual-studio-2012filesystemwatcher

Each time I open Visual Studio the FileSystemWatcher EnableRaisingEvent changes


I'm using C# in Visual Studio 2010 with the framework 4.0.

In my project, in two different forms, there are two FileSystemWatchers with the property EnableRaisingEvent set to false. If I close Visual Studio, when I reopen it I get in both FileSystemWatcher the property EnableRaisingEvent set to true.

In both my forms in the designer file there is the following code:

private void InitializeComponent()
{
     this.components = new System.ComponentModel.Container();
     System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
     this.fileSystemWatcher1 = new System.IO.FileSystemWatcher();
     ((System.ComponentModel.ISupportInitialize)(this.fileSystemWatcher1)).BeginInit();   
     this.SuspendLayout();

     this.fileSystemWatcher1.Filter = "my_filter";
     this.fileSystemWatcher1.NotifyFilter = System.IO.NotifyFilters.LastWrite;
     this.fileSystemWatcher1.SynchronizingObject = this;
     this.fileSystemWatcher1.Changed += new System.IO.FileSystemEventHandler(this.fileSystemWatcher1_Changed);
}

The property EnableRaisingEvent is not set, but the default is false.

Any idea why I get this strange behaviour?

edit

I followed the Virtlink's suggestion, adding the following line of code:

this.fileSystemWatcher1.EnableRaisingEvents = false;

It seemed to solve my problem, but after a few days (and some opening, closing and rebuilding of the project, but without modifying the fileSystemWatcher1) I found:

  • in the designer, in the properties of the fileSystemWatcher1, EnableRaisingEvents was set back to true

  • in the code, the line previously added was missing

I tried moving to Visual Studio 2012 (still framework 4.0) and the workaround fixed the problem for some more days. Then I got the same situation as in VS10.

Any other idea?


Solution

  • It also happens in Visual Studio 2012, and you don't have to close Visual Studio. Reopening the form designer is enough to get the property to be set to True, both visually in the designer and at run-time.

    It seems to be a bug in the FileSystemWatcher.

    A workaround is to add this line to your InitializeComponent explicitly:

    this.fileSystemWatcher1.EnableRaisingEvents = false;
    

    If the designer will not work with you, you'll have to work against it. Anything you put in InitializeComponent might be overwritten or removed by the designer, as InitializeComponent is the designer's territory. One way to deal with this is to add the line right after the call to InitializeComponent in your form's constructor.

    InitializeComponent();
    this.fileSystemWatcher1.EnableRaisingEvents = false;
    

    This means the designer will not show you the correct value for EnableRaisingEvents, but since it didn't work all that well anyway that might not be such a big problem. Putting the line in the constructor ensures it is not removed by the designer at any point in the future.