简体   繁体   中英

FileSystemWatcher not working for existing files in a directory

I have written code for watching a file & whenever changes is made fire the event notifying user of changes. However it doesn't fire for the files already in a directory but in the same directory if I create a new file and make changes to it, works like a charm.

How to make it work for the files already existing in a particular directory?

private static void Watcher()
{
 using(FileSystemWatcher watcher = new FileSystemWatcher(path,filter))
 {
  watcher.NotifyFilter = NotifyFilters.LastAccess;
  watcher.Changed += OnChanged;
  watcher.EnableRaisingEvents = true;

  Console.ReadKey();
 }
}

private static void OnChanged(object sender, FileSystemEventArgs e)
{
  if (e.ChangeType != WatcherChangeTypes.Changed)
  {
    return;
  }
  Console.WriteLine($"Changed: {e.FullPath}");
}

The issue is related with file extension which shows as "text document" in explorer window and when I checked in propertied its ".LOG". Modifying in the filter property fixed it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM