Exemplo n.º 1
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));
        }
Exemplo n.º 2
0
        public string MakeFile(string currentDirectory, string fileName)
        {
            currentDirectory = NormalizeCurrentDirectory(currentDirectory);
            if (!PathUtils.IsAbsolutePath(fileName))
            {
                fileName = PathUtils.CombinePath(currentDirectory, fileName);
            }

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

            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.AddChildFile(directoryParts[directoryParts.Length - 1]);
            return(fileName);
        }