Why FileSystemWatcher doesn't work in Linux container watching Windows volume

后端 未结 1 1522
心在旅途
心在旅途 2021-02-04 14:49

Given the program:

using System;
using System.IO;

namespace fsw_bug_poc
{
    class Program
    {
        private static          


        
1条回答
  •  -上瘾入骨i
    2021-02-04 14:59

    Switching to PhysicalFileProvider.Watch did the job. It seems to be a more portable implementation for file system watching strategies.

    The current implementation of PhysicalFileProvider supports the DOTNET_USE_POLLING_FILE_WATCHER environment variable. I couldn't find any reference of it in FileSystemWatcher implementation.

    using System;
    using System.IO;
    
    namespace fsw_bug_poc
    {
        class Program
        {
            private static PhysicalFileProvider _fileProvider;
    
            static void Main(string[] args)
            {
                _fileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "Watched"));
                WatchForFileChanges();
                
                Console.ReadKey(false);
            }           
    
            private void WatchForFileChanges()
            {
                _fileChangeToken = _fileProvider.Watch("*.*");
                _fileChangeToken.RegisterChangeCallback(Notify, default);
            }
    
            private void Notify(object state)
            {
                Console.WriteLine("File change detected");
                WatchForFileChanges();
            }
        }
    }
    

    0 讨论(0)
提交回复
热议问题