Copy entire contents of a directory in C#
This post will discuss how to synchronously copy the entire contents of a directory to another location in C#.
The DirectoryInfo Class provides instance methods for common I/O operations like creating, copying, moving, renaming, and deleting directories. The following example, taken from MSDN, demonstrates how to copy a directory and its contents to a new location.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
using System; using System.IO; class CopyDir { public static void CopyAll(DirectoryInfo source, DirectoryInfo target) { if (source.FullName.ToLower() == target.FullName.ToLower()) { return; } // Check if the target directory exists, if not, create it. if (Directory.Exists(target.FullName) == false) { Directory.CreateDirectory(target.FullName); } // Copy each file into it's new directory. foreach (FileInfo fi in source.GetFiles()) { Console.WriteLine(@"Copying {0}\{1}", target.FullName, fi.Name); fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true); } // Copy each subdirectory using recursion. foreach (DirectoryInfo diSourceSubDir in source.GetDirectories()) { DirectoryInfo nextTargetSubDir = target.CreateSubdirectory(diSourceSubDir.Name); CopyAll(diSourceSubDir, nextTargetSubDir); } } public static void Main() { string srcDir = @"C:\srcDir"; string destDir = @"C:\destDir"; DirectoryInfo diSource = new DirectoryInfo(srcDir); DirectoryInfo diTarget = new DirectoryInfo(destDir); CopyAll(diSource, diTarget); } } |
The following example from MSDN, copies subdirectories by setting the recursive parameter of the CopyDirectory() method to true. The solution recursively copies each subdirectory, until all subdirectories are processed.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
using System; using System.IO; class CopyDir { static void CopyDirectory(string srcDir, string destDir, bool recursive) { // Get information about the source directory var dir = new DirectoryInfo(srcDir); // Check if the source directory exists if (!dir.Exists) { throw new DirectoryNotFoundException($"Source directory not found: {dir.FullName}"); } // Cache directories before we start copying DirectoryInfo[] dirs = dir.GetDirectories(); // Create the destination directory Directory.CreateDirectory(destDir); // Get the files in the source directory and copy to the destination directory foreach (FileInfo file in dir.GetFiles()) { string targetFilePath = Path.Combine(destDir, file.Name); file.CopyTo(targetFilePath); } // If recursive and copying subdirectories, recursively call this method if (recursive) { foreach (DirectoryInfo subDir in dirs) { string newDestinationDir = Path.Combine(destDir, subDir.Name); CopyDirectory(subDir.FullName, newDestinationDir, true); } } } public static void Main() { string srcDir = @"C:\srcDir"; string destDir = @"C:\destDir"; CopyDirectory(srcDir, destDir, true); } } |
That’s all about synchronously copying the entire contents of a directory to another location in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)