Пример #1
0
        public static IEnumerable <FileSystemItem> GetItems(this DirectoryItem directoryItem, Predicate <FileSystemItem> predicate)
        {
            var elements = new Stack <DirectoryItem>();

            elements.Push(directoryItem);

            while (elements.Any())
            {
                var currentElement = elements.Pop();

                if (predicate(currentElement))
                {
                    yield return(currentElement);
                }

                foreach (var item in currentElement.Elements)
                {
                    if (predicate(item))
                    {
                        yield return(item);
                    }

                    if (item.GetType() == typeof(DirectoryItem))
                    {
                        elements.Push((DirectoryItem)item);
                    }
                }
            }
        }
        public FileSystemBuilder AddDirectory(string name)
        {
            var dir = new DirectoryItem(name);

            this.currentDirectory.Add(dir);
            this.currentDirectory = dir;
            return(this);
        }
        public FileSystemBuilder SetCurrentDirectory(string directoryName)
        {
            var elements = new Stack <DirectoryItem>();

            elements.Push(Root);

            while (elements.Any())
            {
                var currentElement = elements.Pop();

                if (currentElement.Name == directoryName)
                {
                    currentDirectory = currentElement;
                    return(this);
                }

                foreach (var directoryItem in currentElement.Elements.OfType <DirectoryItem>())
                {
                    elements.Push(directoryItem);
                }
            }

            throw new Exception("Directory not found");
        }
 public FileSystemBuilder(string rootDirectory)
 {
     Root             = new DirectoryItem(rootDirectory);
     currentDirectory = Root;
 }