Skip to content

FileSystemWatcher does not detect watched directory deletion on Linux #126295

@svick

Description

@svick

Description

On Windows, when a directory watched by FileSystemWatcher is deleted, an error is raised that notifies the user of this. But on Linux, the user has no way of learning about it. From cursory reading of inotify docs, FileSystemWatcher could listen to the IN_DELETE_SELF event to make this work.

Reproduction Steps

Run the following code:

string dir = Path.Combine(Path.GetTempPath(), "fsw-demo");
if (Directory.Exists(dir)) Directory.Delete(dir, true);
Directory.CreateDirectory(dir);

using var watcher = new FileSystemWatcher(dir);
watcher.Created += (_, e) => Console.WriteLine($"Created: {e.Name}");
watcher.Deleted += (_, e) => Console.WriteLine($"Deleted: {e.Name}");
watcher.Error   += (_, e) => Console.WriteLine($"Error:   {e.GetException()}");
watcher.EnableRaisingEvents = true;

File.WriteAllText(Path.Combine(dir, "file.txt"), "text");

Directory.Delete(dir, true);

await Task.Delay(1000);

try { Directory.Delete(dir, true); } catch { }

Expected behavior

On Windows, it prints:

Created: file.txt
Error:   System.ComponentModel.Win32Exception (5): Access is denied.

Actual behavior

On Linux (WSL), it prints instead:

Created: file.txt
Deleted: file.txt

Regression?

No.

Configuration

  • .Net 11 Preview 2 on both OSes
  • Ubuntu 22.04.1 LTS on WSL

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.IOuntriagedNew issue has not been triaged by the area owner

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions