void ev_FileChanged(DirectoryWatcher source, string fullpath) { if (!IsFileOk(fullpath)) { return; } // Logger.Trace("filechnaged: {0}", fullpath); if (File.Exists(fullpath)) { lock (_paths) { string f = TrimPath(fullpath); if (_paths.Contains(f)) { OnPathChanged(f); } else { _paths.Add(f); OnPathAdded(f); } } } else { lock (_paths) { string f = TrimPath(fullpath); _paths.Remove(f); OnPathRemoved(f); } } }
public virtual void Dispose() { if (_fswatcher != null) { try { _fswatcher.Dispose(); } catch { } _fswatcher = null; } _paths = null; }
public void Go(List <string> paths) { _dir = this.Config.ScanDirectoryAbsolute; _dir = _dir.Trim(); while (_dir.Length > 0 && _dir[_dir.Length - 1] == Path.DirectorySeparatorChar) { _dir = _dir.Substring(0, _dir.Length - 1); } _fswatcher = new DirectoryWatcher(_dir); _fswatcher.EnableWatchingContents = true; _fswatcher.Initialize(); _fswatcher.SubdirectoryChanged += ev_SubdirChanged; if (paths != null) { // grepmatcher does this _paths = paths; Logger.Trace("starting initial load of {0}", this.Config.Name); } else { // pathmatcher does this Logger.Trace("starting initial scan of {0}", this.Config.Name); var sw = new System.Diagnostics.Stopwatch(); sw.Start(); foreach (DirectoryEntry entry in FastDirectoryScanner.RecursiveScan(_dir, skipdir => !IsFileOk(TrimPath(skipdir), true))) { if (entry.IsFile) { string tp = TrimPath(entry.FullPath); if (IsFileOk(tp)) { _paths.Add(tp); } } } sw.Stop(); Logger.Trace("[{0}ms] {1} paths found on initial scan of {2}", sw.ElapsedMilliseconds, _paths.Count, this.Config.Name); } OnPathsInited(); }
void ev_SubdirChanged(DirectoryWatcher source, string fulldirpath) { string dirpath = TrimPath(fulldirpath); if (dirpath.Length > 0 && dirpath[dirpath.Length - 1] != Path.DirectorySeparatorChar) { dirpath += Path.DirectorySeparatorChar; } if (!Directory.Exists(fulldirpath)) { // Logger.Trace("subdir removed: {0}", dirpath); lock (_paths) { int i = 0; while (i < _paths.Count) { string f = _paths[i++]; if (f.StartsWith(dirpath)) { _paths.RemoveAt(--i); OnPathRemoved(f); } } } } else { // Logger.Trace("subdir changed: {0}", dirpath); HashSet <string> filesindir = new HashSet <string>(Directory.GetFiles(fulldirpath).Where(x => IsFileOk(x)).Select(x => TrimPath(x))); lock (_paths) { int i = 0; while (i < _paths.Count) { string path = _paths[i++]; string dir = Path.GetDirectoryName(path); if (dir.Length > 0 && dir[dir.Length - 1] != Path.DirectorySeparatorChar) { dir += Path.DirectorySeparatorChar; } if (dir == dirpath) { if (filesindir.Contains(path)) { OnPathChanged(path); } else { _paths.RemoveAt(--i); OnPathRemoved(path); } filesindir.Remove(path); } } foreach (string f in filesindir) { _paths.Add(f); OnPathAdded(f); } } } }