实时观察本地文件变化

偶尔看到的代码,记录一下

.Net 6.0 , C# 11

           using (var watcher = new ObservableFileSystemWatcher(c => { c.Path = @$".{Path.DirectorySeparatorChar}Sources"; }))
            {
                var changes = watcher.Changed.Throttle(TimeSpan.FromSeconds(.5)).Where(c => c.FullPath.EndsWith(@"DynamicProgram.cs")).Select(c => c.FullPath);

                changes.Subscribe(filepath => runner.Execute(compiler.Compile(filepath), new[] { "France" }));

                watcher.Start();

                Console.WriteLine("Press any key to exit!");
                Console.ReadLine();
            }
    /// 
    ///     This is a wrapper around a file system watcher to use the Rx framework instead of event handlers to handle
    ///     notifications of file system changes.
    /// 
    public class ObservableFileSystemWatcher : IDisposable
    {
        public readonly FileSystemWatcher Watcher;

        public IObservable Changed { get; private set; }
        public IObservable Renamed { get; private set; }
        public IObservable Deleted { get; private set; }
        public IObservable Errors { get; private set; }
        public IObservable Created { get; private set; }

        /// 
        ///     Pass an existing FileSystemWatcher instance, this is just for the case where it's not possible to only pass the
        ///     configuration, be aware that disposing this wrapper will dispose the FileSystemWatcher instance too.
        /// 
        /// 
        public ObservableFileSystemWatcher(FileSystemWatcher watcher)
        {
            Watcher = watcher;

            Changed = Observable
                .FromEventPattern(h => Watcher.Changed += h, h => Watcher.Changed -= h)
                .Select(x => x.EventArgs);

            Renamed = Observable
                .FromEventPattern(h => Watcher.Renamed += h, h => Watcher.Renamed -= h)
                .Select(x => x.EventArgs);

            Deleted = Observable
                .FromEventPattern(h => Watcher.Deleted += h, h => Watcher.Deleted -= h)
                .Select(x => x.EventArgs);

            Errors = Observable
                .FromEventPattern(h => Watcher.Error += h, h => Watcher.Error -= h)
                .Select(x => x.EventArgs);

            Created = Observable
                .FromEventPattern(h => Watcher.Created += h, h => Watcher.Created -= h)
                .Select(x => x.EventArgs);
        }

        /// 
        ///     Pass a function to configure the FileSystemWatcher as desired, this constructor will manage creating and applying
        ///     the configuration.
        /// 
        public ObservableFileSystemWatcher(Action configure)
            : this(new FileSystemWatcher())
        {
            configure(Watcher);
        }

        public void Start()
        {
            Watcher.EnableRaisingEvents = true;
        }

        public void Stop()
        {
            Watcher.EnableRaisingEvents = false;
        }

        public void Dispose()
        {
            Watcher.Dispose();
        }
    }

你可能感兴趣的:(WinForm,c#)