实时观察本地文件变化

Posted jjhua

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实时观察本地文件变化相关的知识,希望对你有一定的参考价值。

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

.Net 6.0 , C# 11

           using (var watcher = new ObservableFileSystemWatcher(c =>  c.Path = @$".Path.DirectorySeparatorCharSources"; ))
            
                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();
            
    /// <summary>
    ///     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.
    /// </summary>
    public class ObservableFileSystemWatcher : IDisposable
    
        public readonly FileSystemWatcher Watcher;

        public IObservable<FileSystemEventArgs> Changed  get; private set; 
        public IObservable<RenamedEventArgs> Renamed  get; private set; 
        public IObservable<FileSystemEventArgs> Deleted  get; private set; 
        public IObservable<ErrorEventArgs> Errors  get; private set; 
        public IObservable<FileSystemEventArgs> Created  get; private set; 

        /// <summary>
        ///     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.
        /// </summary>
        /// <param name="watcher"></param>
        public ObservableFileSystemWatcher(FileSystemWatcher watcher)
        
            Watcher = watcher;

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

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

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

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

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

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

        public void Start()
        
            Watcher.EnableRaisingEvents = true;
        

        public void Stop()
        
            Watcher.EnableRaisingEvents = false;
        

        public void Dispose()
        
            Watcher.Dispose();
        
    

以上是关于实时观察本地文件变化的主要内容,如果未能解决你的问题,请参考以下文章

如何实现一个 Firebase 监听器来观察数据库的实时变化

如何观察 Firebase 实时数据库的变化

核心数据 - 观察变化并注册本地通知

任务8:08_尚硅谷_Flume案例_监控本地变化文件(需求分析)

永远观察实时数据的片段

如何通过 conda 安装我自己的 python 模块(包)并观察它的变化