public FileSystemWatcher CreateDirectoryWatcher(string file) { var watcher = new FileSystemWatcher(); watcher.Path = file; watcher.NotifyFilter = NotifyFilters.DirectoryName; watcher.IncludeSubdirectories = true; var observer = Observable .FromEvent <FileSystemEventHandler, FileSystemEventArgs>( c => c.Invoke, h => { watcher.Created += h; watcher.Deleted += h; }, h => { watcher.Created -= h; watcher.Deleted -= h; }) .Do(x => { var path = GetPathFromEvent(x); if (x.EventArgs.ChangeType == WatcherChangeTypes.Deleted) { OnApplicationDeletion(path); } else { OnNewApplication(path); } }) .Subscribe(); SystemObservers.Add(observer); watcher.EnableRaisingEvents = true; return(watcher); }
public FileSystemWatcher CreateFileWatcher(string file) { var watcher = new FileSystemWatcher(); watcher.Path = file; watcher.NotifyFilter = NotifyFilters.LastWrite; watcher.IncludeSubdirectories = true; watcher.InternalBufferSize = BUFFER_64K; var observer = Observable .FromEvent <FileSystemEventHandler, FileSystemEventArgs>( c => c.Invoke, h => { watcher.Changed += h; watcher.Deleted += h; }, h => { watcher.Changed -= h; watcher.Deleted -= h; }) .Where(x => !string.IsNullOrEmpty(Path.GetExtension(x.EventArgs.FullPath))) .Throttle(TimeSpan.FromSeconds(5)) .Do(x => { var path = GetPathFromEvent(x); if (x.EventArgs.ChangeType != WatcherChangeTypes.Deleted) { OnApplicationChange(path); } }) .Subscribe(); SystemObservers.Add(observer); watcher.EnableRaisingEvents = true; return(watcher); }