title: How to use FileSystemWatcher on Linux with .NET 5.0+
body: <p>I'm trying to watch for changes in a specified directory using the FileSystemWatcher class. Based heavily off of the example on the <a href="https://docs.microsoft.com/en-us/dotnet/api/system.io.filesystemwatcher?view=net-5.0#examples" rel="nofollow noreferrer">MS docs</a> which works fine on my windows machine. The problem though, I'm targeting Ubtuntu 18.04 lts and greater for running this. The <em>watcher</em> 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.</p>
  <p>Are there any undocumented or &quot;hidden&quot; 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 &quot;portably.&quot;
  Any tips/details would be appreciated as I'm sure others have come across this as well, or are at least likely to.</p>
  <p>Current code that works with Windows build but not Linux:</p>
  <pre><code>public void WatchFolder()
  {
      string directoryPath = _configuration[&quot;FolderLocation&quot;];
  
      if (string.IsNullOrWhiteSpace(directoryPath))
          throw new Exception(&quot;Folder location was not set.&quot;);
  
      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($&quot;Couldn't watch folder: {directoryPath}\nBecause: {ex}&quot;);
      }
  }
  </code></pre>
  <p><strong>Edit</strong></p>
  <p>Attempting to use the answer in this related SO quetion: <a href="https://stackoverflow.com/a/57025115/8645002">https://stackoverflow.com/a/57025115/8645002</a> did not fix the issue. The delegate still doesn't trigger when running the built exe in a Linux environment.</p>
  <p>note: building for Linux x64 single file self-contained exe.</p>