private static void FileSystemVisitorOnFileFinded(object sender, ItemFindedEventArgs args) { //System.Console.WriteLine($"File : { args.Path } "); //if (args.Path.Contains("Installer")) // args.ExcludeItem = true; args.ShouldStopSearch = true; }
public ActionType ProcessItemFinded <TItemInfo>( TItemInfo itemInfo, Func <FileSystemInfo, bool> filter, EventHandler <ItemFindedEventArgs <TItemInfo> > itemFinded, EventHandler <ItemFindedEventArgs <TItemInfo> > filteredItemFinded, Action <EventHandler <ItemFindedEventArgs <TItemInfo> >, ItemFindedEventArgs <TItemInfo> > eventEmitter) where TItemInfo : FileSystemInfo { ItemFindedEventArgs <TItemInfo> args = new ItemFindedEventArgs <TItemInfo> { FindedItem = itemInfo, ActionType = ActionType.ContinueSearch }; eventEmitter(itemFinded, args); if (args.ActionType != ActionType.ContinueSearch || filter == null) { return(args.ActionType); } if (filter(itemInfo)) { args = new ItemFindedEventArgs <TItemInfo> { FindedItem = itemInfo, ActionType = ActionType.ContinueSearch }; eventEmitter(filteredItemFinded, args); return(args.ActionType); } return(ActionType.SkipElement); }
private IEnumerable <FileSystemInfo> GetAllSubFileSystemInfo(DirectoryInfo directory) { IEnumerable <FileSystemInfo> directories; try { directories = directory.EnumerateFileSystemInfos(); } catch (UnauthorizedAccessException) { throw new UnauthorizedAccessException($"You don't have access to the {directory.FullName} directory"); } foreach (var fileSystemInfo in directories) { FileInfo fileInfo = fileSystemInfo as FileInfo; if (fileInfo != null) { ItemFindedEventArgs <FileInfo> fileEventArgs = new ItemFindedEventArgs <FileInfo> { FindedItem = fileInfo, ActionType = Action.ContinueSearch }; OnEvent(FileFinded, fileEventArgs); //It Checks what the consumer want to do with the founded file if (fileEventArgs.ActionType == Action.StopSearch) { yield break; } else if (fileEventArgs.ActionType == Action.SkipElement) { continue; } else if (FileSystemInfoFilter(fileInfo, FilteredFileFinded)) { yield return(fileInfo); } } DirectoryInfo directoryInfo = fileSystemInfo as DirectoryInfo; if (directoryInfo != null) { ItemFindedEventArgs <DirectoryInfo> directoryEventArgs = new ItemFindedEventArgs <DirectoryInfo> { FindedItem = directoryInfo, ActionType = Action.ContinueSearch }; OnEvent(DirectoryFinded, directoryEventArgs); //It Checks what the consumer want to do with the founded directory if (directoryEventArgs.ActionType == Action.StopSearch) { yield break; } else if ((directoryEventArgs.ActionType == Action.ContinueSearch) && FileSystemInfoFilter(directoryInfo, FilteredDirectoryFinded)) { yield return(directoryInfo); } foreach (var innerInfo in GetAllSubFileSystemInfo(directoryInfo)) { yield return(innerInfo); } } } }