コード例 #1
0
        public ObservableFileSystemWatcher(string path, string filter = null)
        {
            _fileSystemWatcher = string.IsNullOrWhiteSpace(filter)
                ? new FileSystemWatcher(path)
                : new FileSystemWatcher(path, filter);

            Changed = Observable.FromEventPattern<FileSystemEventHandler, FileSystemEventArgs>
                (h => (s, e) => h(s, e),
                 h => _fileSystemWatcher.Changed += h,
                 h => _fileSystemWatcher.Changed -= h)
                .Select(e => e.EventArgs);

            Created = Observable.FromEventPattern<FileSystemEventHandler, FileSystemEventArgs>
                (h => (s, e) => h(s, e),
                 h => _fileSystemWatcher.Created += h,
                 h => _fileSystemWatcher.Created -= h)
                .Select(e => e.EventArgs);

            Deleted = Observable.FromEventPattern<FileSystemEventHandler, FileSystemEventArgs>
                (h => (s, e) => h(s, e),
                 h => _fileSystemWatcher.Deleted += h,
                 h => _fileSystemWatcher.Deleted -= h)
                .Select(e => e.EventArgs);

            Errors = Observable.FromEventPattern<ErrorEventHandler, ErrorEventArgs>
                (h => (s, e) => h(s, e),
                 h => _fileSystemWatcher.Error += h,
                 h => _fileSystemWatcher.Error -= h)
                .Select(e => e.EventArgs);

            Renamed = Observable.FromEventPattern<RenamedEventHandler, RenamedEventArgs>
                (h => (s, e) => h(s, e),
                 h => _fileSystemWatcher.Renamed += h,
                 h => _fileSystemWatcher.Renamed -= h)
                .Select(e => e.EventArgs);

            _fileSystemWatcher.DisposeWith(this);
        }