2

I have the following FileSystemWatcher setup in my windows service.

    FileSystemWatcher Watcher = new FileSystemWatcher();
    Watcher.Path = watcherDir;
    Watcher.NotifyFilter = NotifyFilters.LastWrite;
    Watcher.Renamed += Watcher_Renamed;
    Watcher.Changed += Watcher_Changed;
    Watcher.Error += Watcher_Error;
    Watcher.Filter = "*.*";
    Watcher.IncludeSubdirectories = false;
    Watcher.EnableRaisingEvents = true;

I see some inconsistent behavior - it listens to changes in subdirectories too. I have logged to see what it finds it is bit odd.
I am watching C:\Temp\Folder1, and some other process creates a log file in C:\Temp\Folder1\Folder2. This FileSystemWatcher object is picking this info up -
1. e.FullPath gives the name of the subdirectory, in this case C:\Temp\Folder1\Folder2
2. Path.GetDirectoryName(e.FullPath) gives me the directory that I am actually watching for i.e. C:\Temp\Folder1.
3. The extension is empty and that is how I ignore this and this is how I ignore this case.

Any suggestions for how else I can figure out what is going on here?

1 Answers1

4

The creation or deletion of a file within a directory is also counted as a change to that directory itself. That's the event that's being reported to you. Folder2, itself, is within the directory that you're monitoring.

The reported path is the path of the directory, not the file within it, and you'll notice that its your Changed handler being invoked, despite the fact that the file system operations are actually creation or deletion.

In your event handler, you could just check whether the reported path is a directory, and just return and perform no further processing in the event handler, if that's the case.

Community
  • 1
  • 1
Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
  • Yes, I am already checking that, but shouldn't Watcher.IncludeSubdirectories = false; prevent this from even occurring? –  Apr 04 '14 at 10:50
  • 1
    @Kodathon - no. Think of directories as just being special files that contain a list of files and where to find them on the file system. When a file is added or removed from a directory, that special file, itself, is being changed. And it's that special file, sitting within the `Folder1` directory, the one that you're monitoring, that you're receiving events for. You *don't* receive events when a file, *within* `Folder2` is changed. Just when files are added or removed from the directory. – Damien_The_Unbeliever Apr 04 '14 at 10:53
  • Would have preferred if that property handled it. Anyways, will go with this extra check to verify the path. Thanks ! –  Apr 04 '14 at 11:05
  • @Kodathon - consider if you were working with the `Created` event instead. It would be (to my mind) seriously unexpected if you said "I don't want to know about creation events in subdirectories" and that meant that you didn't get told about new directories being created in the directory you're monitoring. – Damien_The_Unbeliever Apr 04 '14 at 11:07
  • If I am explicitly specifying that I am not keen on keeping track of files within the subdirectories, the additional notifications are an overhead to me. Apart from having the extra check, there is this issue of buffer size which I definitely don't want due to the subdirectories which I am not concerned about to begin with. –  Apr 04 '14 at 11:13