FileSystemWatcher Changed event is raised twice

Posted by user214707 on Stack Overflow See other posts from Stack Overflow or by user214707
Published on 2009-11-19T16:55:08Z Indexed on 2012/06/15 15:17 UTC
Read the original article Hit count: 542

Filed under:
|

I have an application where I am looking for a text file and if there are any changes made to the file I am using the OnChanged eventhandler to handle the event. I am using the NotifyFilters.LastWriteTime but still the event is getting fired twice. Here is the code.

public void Initialize()
{
   FileSystemWatcher _fileWatcher = new FileSystemWatcher();
  _fileWatcher.Path = "C:\\Folder";
  _fileWatcher.NotifyFilter = NotifyFilters.LastWrite;
  _fileWatcher.Filter = "Version.txt";
  _fileWatcher.Changed += new FileSystemEventHandler(OnChanged);
  _fileWatcher.EnableRaisingEvents = true;
}

private void OnChanged(object source, FileSystemEventArgs e)
{
   .......
}

In my case the OnChanged is called twice, when I change the text file version.txt and save it.

© Stack Overflow or respective owner

Related posts about c#

Related posts about filesystemwatcher