Beispiel #1
0
        private IEnumerable <FileInfoBase> GetFilteredFiles(DirectoryInfoBase directory)
        {
            var breakSearch = false;

            foreach (var file in directory.EnumerateFiles())
            {
                if (Status == FileSystemVisitorStatus.Stopped)
                {
                    break;
                }

                var excludeItem = false;
                FileFoundEvent?.Invoke(file, ref breakSearch, ref excludeItem);
                if (Filter != null && !Filter.Invoke(file))
                {
                    continue;
                }
                FilterItemActions(file, ref breakSearch, ref excludeItem);
                if (breakSearch)
                {
                    Status = FileSystemVisitorStatus.Stopped;
                }
                if (!excludeItem)
                {
                    yield return(file);
                }
            }
        }
        /// <summary>
        /// Returns directories and files that are stored in the specified directory
        /// </summary>
        /// <param name="dir">Start directory</param>
        /// <param name="cancelRecursionFlag">Flag to cancel searching</param>
        /// <returns>Directories and files that are stored in the specified directory</returns>
        private IEnumerable <FileSystemInfoBase> GetDirectoryItems(DirectoryInfoBase dir, ref bool cancelRecursionFlag)
        {
            var result = new List <FileSystemInfoBase>();

            if (cancelRecursionFlag)
            {
                return(result);
            }

            foreach (var directory in dir.EnumerateDirectories())
            {
                var eventParams = new ItemFindedEventArgs {
                    FullName = directory.FullName
                };
                OnDirectoryFinded(eventParams);

                if (eventParams.CancelSearching)
                {
                    cancelRecursionFlag = true;
                    result.Add(directory);
                    return(result);
                }

                if (!eventParams.ExcludeItemFromResult)
                {
                    result.Add(directory);
                }

                result.AddRange(GetDirectoryItems(directory, ref cancelRecursionFlag));
                if (cancelRecursionFlag)
                {
                    return(result);
                }
            }
            foreach (var fileInfo in dir.EnumerateFiles())
            {
                var eventParams = new ItemFindedEventArgs {
                    FullName = fileInfo.FullName
                };
                OnFileFinded(eventParams);

                if (eventParams.CancelSearching)
                {
                    cancelRecursionFlag = true;
                    result.Add(fileInfo);
                    return(result);
                }

                if (!eventParams.ExcludeItemFromResult)
                {
                    result.Add(fileInfo);
                }
            }
            return(result);
        }