Пример #1
0
        public string MakeDirectory(string currentDirectory, string directory)
        {
            currentDirectory = NormalizeCurrentDirectory(currentDirectory);
            if (!PathUtils.IsAbsolutePath(directory))
            {
                directory = PathUtils.CombinePath(currentDirectory, directory);
            }

            string[] directoryParts = PathUtils.SplitPath(directory);

            IFileSystemItem currentItem = this.root;

            for (int i = 0; i < directoryParts.Length - 1; i++)
            {
                currentItem = currentItem.ChildItems.FirstOrDefault(item => FileSystemItemNameComparer.Equals(item.Name, directoryParts[i]));
                if (currentItem is null)
                {
                    throw new FileSystemConsoleException("Destination path is not exists.");
                }
            }

            if (currentItem.Kind != FileSystemItemKind.Volume && currentItem.Kind != FileSystemItemKind.Directory)
            {
                throw new FileSystemConsoleException("Destination path is not a volume or a directory.");
            }

            currentItem.AddChildDirectory(directoryParts[directoryParts.Length - 1]);
            return(directory);
        }
Пример #2
0
        private static void CopyItemTree(IFileSystemItem item, IFileSystemItem destItem)
        {
            if (item.Kind != FileSystemItemKind.Directory && item.Kind != FileSystemItemKind.File)
            {
                throw new ArgumentException(Invariant($"{nameof(item)} is not a directory or a file."), nameof(item));
            }

            if (destItem.Kind != FileSystemItemKind.Volume && destItem.Kind != FileSystemItemKind.Directory)
            {
                throw new ArgumentException(Invariant($"{nameof(destItem)} is not a volume or directory."), nameof(item));
            }

            IFileSystemItem CopyItem()
            {
                switch (item.Kind)
                {
                case FileSystemItemKind.Directory:
                    return(destItem.AddChildDirectory(item.Name));

                case FileSystemItemKind.File:
                    return(destItem.AddChildFile(item.Name));

                default:
                    throw new ArgumentException(Invariant($"{nameof(item)} is not a directory or a file."), nameof(item));
                }
            }

            IFileSystemItem itemCopy = CopyItem();

            item.ChildItems.ForEach(child => CopyItemTree(child, itemCopy));
        }