Пример #1
0
        public FileWatcher(string path, bool isDirectory)
        {
            string dirname;
            string filename;

            if (isDirectory)
            {
                dirname = path;
                filename = string.Empty;
            }
            else
            {
                dirname = Path.GetDirectoryName(path) ?? string.Empty;
                filename = Path.GetFileName(path) ?? string.Empty;
            }

            _watcher = new FileSystemWatcher(dirname, filename)
            {
                IncludeSubdirectories = isDirectory,
                Filter = isDirectory ? "*.*" : string.Empty,
                NotifyFilter =
                    NotifyFilters.FileName |
                    (isDirectory ? NotifyFilters.DirectoryName : 0) |
                    NotifyFilters.LastWrite
            }.AddTo(_disposable);

            Observable.Merge(
                _watcher.CreatedAsObservable(),
                _watcher.DeletedAsObservable(),
                _watcher.ChangedAsObservable(),
                _watcher.RenamedAsObservable())
                .Throttle(TimeSpan.FromMilliseconds(500))
                .Subscribe(e => FileUpdated?.Invoke(this, e))
                .AddTo(_disposable);
        }