示例#1
0
 /// <summary>
 /// Makes the path absolute to another (absolute) path.
 /// </summary>
 /// <param name="path">The path.</param>
 /// <returns>An absolute path.</returns>
 /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/></exception>
 /// <exception cref="InvalidOperationException">The provided path cannot be relative.</exception>
 public DirectoryPath MakeAbsolute(DirectoryPath path)
 {
     if (path == null)
     {
         throw new ArgumentNullException(nameof(path));
     }
     if (path.IsRelative)
     {
         throw new InvalidOperationException("The provided path cannot be relative.");
     }
     return(IsRelative
         ? path.Combine(this).Collapse()
         : new DirectoryPath(FullPath));
 }
示例#2
0
        /// <summary>
        /// Copies the contents of a directory to the specified location.
        /// </summary>
        /// <example>
        /// <code>
        /// CopyDirectory("source_path", "destination_path");
        /// </code>
        /// </example>
        /// <param name="env">The context.</param>
        /// <param name="source">The source directory path.</param>
        /// <param name="destination">The destination directory path.</param>
        /// <exception cref="ArgumentNullException"><paramref name="env"/> or <paramref name="source"/>
        ///  or <paramref name="destination"/> is <see langword="null"/></exception>
        /// <exception cref="InvalidOperationException">Source directory does not exist or could not be found.</exception>
        public static void CopyDirectory(this IFileSystemEnvironment env, DirectoryPath source,
                                         DirectoryPath destination)
        {
            if (env == null)
            {
                throw new ArgumentNullException(nameof(env));
            }

            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (destination == null)
            {
                throw new ArgumentNullException(nameof(destination));
            }

            if (env.FS == null)
            {
                throw new InvalidOperationException(
                          $"Cannot check if directory exists when {nameof(env)}.{nameof(env.FS)} is null");
            }

            if (source.IsRelative)
            {
                source = source.MakeAbsolute(env);
            }

            // Get the subdirectories for the specified directory.
            var sourceDir = env.FS.GetDirectory(source);

            if (!sourceDir.Exists)
            {
                throw new InvalidOperationException("Source directory does not exist or could not be found: "
                                                    + source.FullPath);
            }

            var dirs = sourceDir.GetDirectories("*", SearchScope.Current);

            var destinationDir = env.FS.GetDirectory(destination);

            if (!destinationDir.Exists)
            {
                destinationDir.Create();
            }

            // Get the files in the directory and copy them to the new location.
            var files = sourceDir.GetFiles("*", SearchScope.Current);

            foreach (var file in files)
            {
                var temppath = destinationDir.Path.CombineWithFilePath(file.Path.GetFilename());
                file.Copy(temppath, true);
            }

            // Copy all of the subdirectories
            foreach (var subdir in dirs)
            {
                var temppath = destination.Combine(subdir.Path.GetDirectoryName());
                CopyDirectory(env, subdir.Path, temppath);
            }
        }