5

I'm trying to watch for changes in a specified directory using the FileSystemWatcher class. Based heavily off of the example on the MS docs which works fine on my windows machine. The problem though, I'm targeting Ubtuntu 18.04 lts and greater for running this. The watcher apparently sets up just fine (code executes and console/logging messages are printed out) but none of the file system events are ever triggered, ie: Change/Create/Delete/Rename/etc nothing triggers those.

Are there any undocumented or "hidden" setup details needed to work with Linux, or is this not supported altogether and even though it's in .NET 5 shouldn't be used for Linux/Mac/Unix builds? I can't find any definitive docs on non-Windows usage, but thought that .NET 5 was meant to work "portably." Any tips/details would be appreciated as I'm sure others have come across this as well, or are at least likely to.

Current code that works with Windows build but not Linux:

public void WatchFolder()
{
    string directoryPath = _configuration["FolderLocation"];

    if (string.IsNullOrWhiteSpace(directoryPath))
        throw new Exception("Folder location was not set.");

    try
    {
        // setup file system watcher
        using var watcher = new FileSystemWatcher(directoryPath);
        watcher.NotifyFilter = NotifyFilters.Attributes
                         | NotifyFilters.CreationTime
                         | NotifyFilters.DirectoryName
                         | NotifyFilters.FileName
                         | NotifyFilters.LastAccess
                         | NotifyFilters.LastWrite
                         | NotifyFilters.Security
                         | NotifyFilters.Size;

        // add handler methods
        watcher.Changed += OnChanged;
        watcher.Created += OnCreated;
        watcher.Deleted += OnDeleted;
        watcher.Renamed += OnRenamed;
        watcher.Error += OnError;

        watcher.IncludeSubdirectories = true;
        watcher.EnableRaisingEvents = true;

        // wait for next key to kill execution
        Console.ReadLine();
    }
    catch (Exception ex)
    {
        _logger.LogError($"Couldn't watch folder: {directoryPath}\nBecause: {ex}");
    }
}

Edit

Attempting to use the answer in this related SO quetion: https://stackoverflow.com/a/57025115/8645002 did not fix the issue. The delegate still doesn't trigger when running the built exe in a Linux environment.

note: building for Linux x64 single file self-contained exe.

CoderLee
  • 3,079
  • 3
  • 25
  • 57
  • Try setting `watcher.IncludeSubdirectories = false;`. But test this: [Microsoft.Extensions.FileProviders.Physical](https://www.nuget.org/packages/microsoft.extensions.fileproviders.physical/) – Jimi Apr 22 '21 at 14:26
  • What if I want to watch sub directories though, I would then need to make a new watcher for every sub dir? – CoderLee Apr 22 '21 at 14:28
  • The test is to verify whether the problem still exists, the NuGet package is the replacement in case the tests gives the results expected. – Jimi Apr 22 '21 at 14:30
  • Ok, so it made no difference when doing that. @Jimi What NuGet package do I want to use now? Not sure I understand you last comment though. – CoderLee Apr 22 '21 at 14:37
  • The one linked in the first comment? – Jimi Apr 22 '21 at 14:38
  • Ah, so I do have that package installed and included. I even tried passing the watcher into a PhysicalFileWatcher with polling enabled. What else should be tried? – CoderLee Apr 22 '21 at 14:40
  • When you call `[PhysicalFileProvider].Watch("*")`, it returns an [IChangeToken](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.primitives.ichangetoken) (store it as a Field), then you call [IChangeToken.RegisterChangeCallback()](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.primitives.ichangetoken.registerchangecallback) (it's an Action delegate, so register a `void` method - don't use a Lambda). The callback notifies a `State` (as an object) when a state in the watched path changes. – Jimi Apr 22 '21 at 15:08
  • What does one do when that doesn't work? I'm building a self-contained Linux exe which doesn't trigger the delegate when run in WSL2 but works fine in debug for Windows. – CoderLee Apr 22 '21 at 15:16

0 Answers0