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

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

                var excludeItem = false;
                var dir         = item as DirectoryInfoBase;
                if (dir != null)
                {
                    DirectoryFoundEvent?.Invoke(dir, ref breakSearch, ref excludeItem);
                    if (!breakSearch && !excludeItem)
                    {
                        foreach (var i in GetFilteredItems(dir)) //going recursion here
                        {
                            yield return(i);
                        }
                    }
                }
                else
                {
                    var file = item as FileInfoBase;
                    if (file != null)
                    {
                        FileFoundEvent?.Invoke(file, ref breakSearch, ref excludeItem);
                    }
                }
                if (breakSearch)
                {
                    Status = FileSystemVisitorStatus.Stopped;
                }
                if (excludeItem)
                {
                    continue;
                }

                if (Filter != null && !Filter.Invoke(item))
                {
                    continue;
                }

                FilterItemActions(item, ref breakSearch, ref excludeItem);
                if (!excludeItem)
                {
                    yield return(item);
                }
                else if (breakSearch)
                {
                    Status = FileSystemVisitorStatus.Stopped;
                }
            }
        }
Beispiel #2
0
        private IEnumerable <FileSystemInfoBase> GetFilteredItemsSecondWay(DirectoryInfoBase directory)
        {
            var breakSearch = false;

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

                var excludeItem = false;
                DirectoryFoundEvent?.Invoke(dir, ref breakSearch, ref excludeItem);
                if (!breakSearch && !excludeItem)
                {
                    foreach (var i in GetFilteredItemsSecondWay(dir)) //going recursion here
                    {
                        yield return(i);
                    }
                }
                if (breakSearch)
                {
                    Status = FileSystemVisitorStatus.Stopped;
                }
                if (excludeItem)
                {
                    continue;
                }
                if (Filter != null && !Filter.Invoke(dir))
                {
                    continue;
                }

                yield return(dir);
            }

            foreach (var file in GetFilteredFiles(directory))
            {
                yield return(file);
            }
        }