New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File System watcher Filters dont work on Linux #21934

danwalmsley opened this Issue Jul 6, 2017 · 4 comments


None yet
5 participants

danwalmsley commented Jul 6, 2017

So when I set:

fileSystemWatcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite;

The created event is raised even when its a folder being created.

Im on ubuntu 17.04,

.NET Command Line Tools (2.0.0-preview2-006497)

Product Information:
 Version:            2.0.0-preview2-006497
 Commit SHA-1 hash:  06a2093335

Runtime Environment:
 OS Name:     ubuntu
 OS Version:  17.04
 OS Platform: Linux
 RID:         linux-x64
 Base Path:   /usr/share/dotnet/sdk/2.0.0-preview2-006497/

Microsoft .NET Core Shared Framework Host

  Version  : 2.0.0-preview2-25407-01
  Build    : 40c565230930ead58a50719c0ec799df77bddee9


This comment has been minimized.


stephentoub commented Jul 6, 2017

Can you please share a standalone repro?


This comment has been minimized.

danwalmsley commented Jul 6, 2017

using System;
using System.IO;
using System.Threading;

namespace WatcherBugRepro
    class Program
        static void Main(string[] args)
            var fileWatcher = new FileSystemWatcher(Directory.GetCurrentDirectory());
            fileWatcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite;
            fileWatcher.EnableRaisingEvents = true;
            fileWatcher.IncludeSubdirectories = true;

            fileWatcher.Created += (sender, e)=>
                Console.WriteLine($"Creating File: {e.FullPath}");

            var folderWatcher = new FileSystemWatcher(Directory.GetCurrentDirectory());
            folderWatcher.NotifyFilter = NotifyFilters.DirectoryName;
            folderWatcher.EnableRaisingEvents = true;
            folderWatcher.IncludeSubdirectories = true;

            folderWatcher.Created += (sender, e) =>
                Console.WriteLine ($"Creating Folder: {e.FullPath}");

            File.CreateText(Path.Combine(Directory.GetCurrentDirectory(), "file.txt"));
            Directory.CreateDirectory(Path.Combine(Directory.GetCurrentDirectory(), "folder"));


output is:

Creating File: /home/dan/repos/WatcherBugRepro/bin/Debug/netcoreapp2.0/folder
Creating Folder: /home/dan/repos/WatcherBugRepro/bin/Debug/netcoreapp2.0/folder

File handler triggered twice when its actually a folder.


This comment has been minimized.


stephentoub commented Jul 8, 2017

@JeremyKuhne JeremyKuhne added this to the 2.1.0 milestone Jul 10, 2017


This comment has been minimized.


ianhays commented Aug 4, 2017

This is a side-effect of us using inotify as the underlying implemenation for FileSystemWatcher on Linux. inotify doesn't have an option to only raise events for files or only raise events for directories, so we have to allow events for both to be raised regardless of NotifyFilters.

However, inotify does tell us that if an event occurred on a directory or on a file after the event has been raised. Since this is after filtering has already been done, we forward that event regardless of the NotifyFilters. I bet we could pass NotifyFilters to the RunningInstance that forwards the events and add in some manual filtering after the fact. It wouldn't quite be the same as filtering through the native call, but it might be adequate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment