// ************************************************************************ // Public Methods // ************************************************************************ #region Public Methods public WatchedRecursiveFileList(DirectoryInfo directory, string fileFilter = "*.3ds") { if (!directory.Exists) directory.Create(); _fileList = new ConcurrentObservableCollection<string>(); _fileFilter = fileFilter; _watchedDirectoryLocation = directory.FullName; _watcher = new FileSystemWatcher(_watchedDirectoryLocation, _fileFilter); _watcher.IncludeSubdirectories = true; _watcher.Filter = _fileFilter; _watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName; _watcher.Changed += new FileSystemEventHandler(watcher_Changed); _watcher.Created += new FileSystemEventHandler(watcher_Changed); _watcher.Deleted += new FileSystemEventHandler(watcher_Changed); _watcher.Renamed += new RenamedEventHandler(watcher_Renamed); _watcher.EnableRaisingEvents = true; _fileList.Clear(); _fileList.Add(""); BuildFileList(null, ""); }
private static void ChildToAggregateMapUnmap <T>(this ConcurrentObservableCollection <T> source, ConcurrentObservableCollection <T> dest, MapUnmap mapUnmap) { NotifyCollectionChangedEventHandler collectionChangedHander = (s, e) => { foreach (var item in e?.OldItems?.OfType <T>() ?? Enumerable.Empty <T>()) { dest.Remove(item); } foreach (var item in e?.NewItems?.OfType <T>() ?? Enumerable.Empty <T>()) { dest.Add(item); } }; using (var freezeToken = source.FreezeUpdates()) { if (mapUnmap == MapUnmap.Map) { dest.AddRange(source); source.CollectionChanged += collectionChangedHander; } else if (mapUnmap == MapUnmap.Unmap) { dest.RemoveRange(source); source.CollectionChanged -= collectionChangedHander; } } }
// ************************************************************************ // Private Methods // ************************************************************************ #region Private Methods private void BuildFileList(DirectoryInfo dir, string directory) { if (dir == null) { dir = new DirectoryInfo(_watchedDirectoryLocation); } FileInfo[] rgFiles = dir.GetFiles(_fileFilter); foreach (FileInfo fi in rgFiles) { _fileList.Add(Path.Combine(directory, fi.Name)); } DirectoryInfo[] subDirs = dir.GetDirectories(); foreach (DirectoryInfo subDir in subDirs) { BuildFileList(subDir, Path.Combine(directory, subDir.Name)); } }