public void Start(WatchSettings options) { var full = new DirectoryInfo(options.Path).FullName; _watcher = new FileSystemWatcher(full, options.Pattern); _watcher.EnableRaisingEvents = true; _watcher.IncludeSubdirectories = options.Recursive; _watcher.Changed += (sender, e) => { HandleWatcherEvent(FileStatus.Changed, e, AcumChanges); }; _watcher.Created += (sender, e) => { HandleWatcherEvent(FileStatus.Created, e, AcumChanges); }; _watcher.Deleted += (sender, e) => { HandleWatcherEvent(FileStatus.Deleted, e, AcumChanges); }; _watcher.Renamed += (sender, e) => { AcumChanges(new FileChange { FullPath = e.FullPath, Name = e.Name, Status = FileStatus.Deleted }); AcumChanges(new FileChange { FullPath = e.FullPath, Name = e.Name, Status = FileStatus.Created }); }; }
public void Watch(WatchSettings options, Action <IEnumerable <FileChange> > onChange) { _timer.AutoReset = false; _timer.Elapsed += (sender, e) => { lock (_unNotifiedChanges) { if (_unNotifiedChanges.Any()) { var changes = _unNotifiedChanges .GroupBy(x => x.FullPath) .Select(x => x.OrderBy(k => k.Status).FirstOrDefault()).ToList(); _unNotifiedChanges.Clear(); try { _runningHandler = true; onChange(changes); } finally { _runningHandler = false; } } } }; Start(options); }
public void Watch(string pattern, Action <IEnumerable <string> > changedPath) { var settings = new WatchSettings { Pattern = pattern, Path = "./", Recursive = true }; Watch(settings, changedPath); }
public static void Watch(this ICakeContext context, WatchSettings settings, Action <IEnumerable <FileChange> > fileChanged) { new ChangeWatcher().Watch(settings, fileChanged); while (Console.ReadLine() != "q") { } }
public void Watch(WatchSettings settings, Action <IEnumerable <string> > changedPath) { var watcher = new FileSystemWatcher { Path = settings.Path, NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite | NotifyFilters.CreationTime, Filter = settings.Pattern, EnableRaisingEvents = true, IncludeSubdirectories = settings.Recursive }; var locker = new object(); var currentSource = new CancellationTokenSource(); var changedFiles = new List <string>(); Func <CancellationTokenSource, bool> isCanceled = c => c == null || c.IsCancellationRequested; Action <FileSystemEventArgs> doWatch = (e) => { if (!isCanceled(currentSource)) { lock (locker) { if (!isCanceled(currentSource)) { currentSource?.Cancel(); currentSource = new CancellationTokenSource(); } changedFiles.Add(e.FullPath); } } // Forces multiple file changes to run the task only once when made in rapid succession. // This prevents the create -> rename VS does from triggering 2 calls while still appearing to be instant. Task.Delay(TimeSpan.FromSeconds(0.25), currentSource.Token) .ContinueWith(t => { List <string> localChanged; lock (locker) { localChanged = new List <string>(changedFiles.Distinct()); changedFiles.Clear(); } changedPath(localChanged); }, currentSource.Token); }; watcher.Changed += (s, e) => doWatch(e); watcher.Created += (s, e) => doWatch(e); watcher.Renamed += (s, e) => doWatch(e); while (Console.ReadLine() != "q") { } }