public static IObservable<string> WatchForChanges(string path,string filter,bool recursive) { var isDirectory = new DirectoryInfo(path).Exists; recursive = isDirectory && recursive; var fileSystemWatcher = new FileSystemWatcher(path) { Filter = filter, IncludeSubdirectories = recursive }; fileSystemWatcher.EnableRaisingEvents = true; var observable = fileSystemWatcher.AsObservable() .ObserveOn(TaskPoolScheduler.Default) .Select(e => e.FullPath) .Buffer(TimeSpan.FromSeconds(1)) .Where(e => e.Any()) .Select(e => e.Distinct()) .SelectMany(e => e); var connectableObservable = observable.Publish(); connectableObservable.Connect(); connectableObservable.Subscribe((v) => log.Info("Detected file system change, root: {0}, change: {1}",path,v), (ex) => fileSystemWatcher.Dispose(), fileSystemWatcher.Dispose); return connectableObservable; }