Stephen Hill's Bloggie Writing about my interests; Programming and Formula One.

DirectoryInfo.CopyTo

One month ago

The .NET Framework contains the DirectoryInfo.MoveTo method which allows you to move a directory, and all of its contents, to another directory. However, when you look at the list of available method in the DirectoryInfo class, you will notice that there is no .CopyTo function. Therefore; you will need to write your own.

To copy a directory from one location to another we must know the following 3 things:

The final thing you must know before you can write your own function is the steps involved in copy a directory and all its files:

  1. Check if the source directory exists.
  2. Check if the destination directory exists.
  3. Create the destination directory.
  4. Copy all of the files.
  5. Goto step 1.

With these in mind, we can create the function.

void CopyDirectory(String Source, String Destination, Boolean Overwrite) {
	// Hold directory information
	var SourceDirectory = new DirectoryInfo(Source);
	var DestinationDirectory = new DirectoryInfo(Destination);
	
	// Throw an error is the source directory does not exist
	if (SourceDirectory.Exists == false) {
		throw new DirectoryNotFoundException();
	}
	
	// Create the destination directory
	if (DestinationDirectory.Exists == false) {
		DestinationDirectory.Create();
	}
	
	// Loop through the files and copy them
	var SubFiles = SourceDirectory.GetFiles();
	for (int i = 0; i < SubFiles.Length; i++) {
		var NewFile = Path.Combine(
			DestinationDirectory.FullName,
			SubFiles[i].Name
		);
		SubFiles[i].CopyTo(NewFile, Overwrite);
	}
	
	// Loop through the directories and call this function
	var SubDirectories = SourceDirectory.GetDirectories();
	for (int i = 0; i < SubDirectories.Length; i++) {
		var NewDirectory = Path.Combine(
			DestinationDirectory.FullName,
			SubDirectories[i].Name
		);
		CopyDirectory(SubDirectories[i].FullName, NewDirectory, Overwrite);
	}
}

Usage:

CopyDirectory(@"C:\Users\", @"C:\Users_Backup\", true);

If you're using .NET 3.0 or higher, you can roll the function up into an extension method.

public static class DirectoryInfoExtensions
{
    public static void CopyTo(this String Source, String Destination, Boolean Overwrite)
    {
		// Hold directory information
		var SourceDirectory = new DirectoryInfo(Source);
		var DestinationDirectory = new DirectoryInfo(Destination);
		
		// Throw an error is the source directory does not exist
		if (SourceDirectory.Exists == false) {
			throw new DirectoryNotFoundException();
		}
		
		// Create the destination directory
		if (DestinationDirectory.Exists == false) {
			DestinationDirectory.Create();
		}
		
		// Loop through the files and copy them
		var SubFiles = SourceDirectory.GetFiles();
		for (int i = 0; i < SubFiles.Length; i++) {
			var NewFile = Path.Combine(
				DestinationDirectory.FullName,
				SubFiles[i].Name
			);
			SubFiles[i].CopyTo(NewFile, Overwrite);
		}
		
		// Loop through the directories and call this function
		var SubDirectories = SourceDirectory.GetDirectories();
		for (int i = 0; i < SubDirectories.Length; i++) {
			var NewDirectory = Path.Combine(
				DestinationDirectory.FullName,
				SubDirectories[i].Name
			);
			SubDirectories[i].CopyTo(NewDirectory, Overwrite);
		}
	}
}

Usage:

var SourceDirectory = new DirectoryInfo(@"C:\Users\");
SourceDirectory.CopyTo(@"C:\Users_Backup\", true);

If you notice anything wrong with the code, please let me know in the comments below and I shall correct it.

Leave a Comment