コード例 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <remarks>
        /// Adapted from: https://docs.microsoft.com/en-us/dotnet/standard/io/how-to-copy-directories
        /// </remarks>
        public static void CopyDirectory(string sourceDirectoryPath, string destinationDirectoryPath)
        {
            // Get the subdirectories for the specified directory.
            var sourceDirectory = new DirectoryInfo(sourceDirectoryPath);

            if (!sourceDirectory.Exists)
            {
                throw new DirectoryNotFoundException($"Source directory does not exist or could not be found:\n{sourceDirectoryPath}");
            }

            var sourceSubDirectories = sourceDirectory.GetDirectories();

            // If the destination directory doesn't exist, create it.
            if (!Directory.Exists(destinationDirectoryPath))
            {
                Directory.CreateDirectory(destinationDirectoryPath);
            }

            // Get the files in the directory and copy them to the new location.
            var files = sourceDirectory.GetFiles();

            foreach (var file in files)
            {
                string destinationFilePath = Path.Combine(destinationDirectoryPath, file.Name);
                file.CopyTo(destinationFilePath, true); // Overwrite files.
            }

            // If copying subdirectories, copy them and their contents to new location.
            foreach (var sourceSubDirectory in sourceSubDirectories)
            {
                string destinationSubDirectoryPath = Path.Combine(destinationDirectoryPath, sourceSubDirectory.Name);
                LocalFileSystem.CopyDirectory(sourceSubDirectory.FullName, destinationSubDirectoryPath);
            }
        }
コード例 #2
0
 public void CopyDirectory(string sourceDirectoryPath, string destinationDirectoryPath)
 {
     LocalFileSystem.CopyDirectory(sourceDirectoryPath, destinationDirectoryPath);
 }