Ejemplo n.º 1
0
        private static IEnumerable <DirectoryInfoBase> GetDirectories(DirectoryInfoBase root, int level, int maxDepth)
        {
            IEnumerable <DirectoryInfoBase> subDirs = null;

            if (maxDepth >= 0 && level > maxDepth)
            {
                yield break;
            }

            try
            {
                subDirs = root.EnumerateDirectories();
            }
            catch (Exception)
            {
                yield break;
            }

            foreach (DirectoryInfoBase dirInfo in subDirs)
            {
                yield return(dirInfo);

                foreach (var recursiveDir in GetDirectories(dirInfo, level + 1, maxDepth))
                {
                    yield return(recursiveDir);
                }
            }
        }
Ejemplo n.º 2
0
        /// <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);
        }
Ejemplo n.º 3
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);
            }
        }
Ejemplo n.º 4
0
        // Normal implementation for IEnumerable<T>
        public IEnumerator <SensorDS18B20> GetEnumerator()
        {
            DirectoryInfoBase devicesDir = fileSystem_.DirectoryInfo.FromDirectoryName("/sys/bus/w1/devices");

            IEnumerable <DirectoryInfoBase> subfolders = null;

            try
            {
                subfolders = devicesDir.EnumerateDirectories("28*");
            }
            catch (DirectoryNotFoundException e)
            {
                throw new OneWireModuleNotLoadedException();
            }

            foreach (var deviceDir in subfolders)
            {
                //TODO it's strange to pass fileSystem in this way. Assign it using some Dependency Injection framework?
                yield return(new SensorDS18B20(deviceDir.Name, fileSystem_));
            }
        }
Ejemplo n.º 5
0
        private static IEnumerable <DirectoryInfoBase> GetDirectories(DirectoryInfoBase root)
        {
            IEnumerable <DirectoryInfoBase> subDirs = null;

            try
            {
                subDirs = root.EnumerateDirectories();
            }
            catch (Exception)
            {
                yield break;
            }

            foreach (DirectoryInfoBase dirInfo in subDirs)
            {
                yield return(dirInfo);

                foreach (var recursiveDir in GetDirectories(dirInfo))
                {
                    yield return(recursiveDir);
                }
            }
        }