How do I copy a directory to a different drive in C#?
In Windows 10 and earlier versions, right-click the folder and select Copy, or click Edit and then Copy. Navigate to the location where you want to place the folder and all its contents. icon on the menu bar. Alternatively, right-click the folder, select Show more options and then Paste.
Similarly, you can copy an entire directory to another directory using cp -r followed by the directory name that you want to copy and the name of the directory to where you want to copy the directory (e.g. cp -r directory-name-1 directory-name-2 ).
Copying Directories with cp Command To copy a directory, including all its files and subdirectories, use the -R or -r option.
You can use this code to perform your operation:
public static void CopyAll(DirectoryInfo source, DirectoryInfo target)
{
// 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);
}
}
below one is also good:
static public void CopyFolder( string sourceFolder, string destFolder )
{
if (!Directory.Exists( destFolder ))
Directory.CreateDirectory( destFolder );
string[] files = Directory.GetFiles( sourceFolder );
foreach (string file in files)
{
string name = Path.GetFileName( file );
string dest = Path.Combine( destFolder, name );
File.Copy( file, dest );
}
string[] folders = Directory.GetDirectories( sourceFolder );
foreach (string folder in folders)
{
string name = Path.GetFileName( folder );
string dest = Path.Combine( destFolder, name );
CopyFolder( folder, dest );
}
}
you can use this function also:
FileSystem.CopyDirectory(sourceDir, destDir);
FileSystem.CopyDirectory(sourceDir, destDir);
FileSystem.CopyDirectory
is in a VB namespace and assembly, but that probably doesn't matter.
How to: Copy, Delete, and Move Files and Folders (C# Programming Guide)
http://msdn.microsoft.com/en-us/library/cc148994.aspx
C# Copy Folder Recursively
http://www.csharp411.com/c-copy-folder-recursively/
Here's an extension that will work in .NET 4.0+
var source = new DirectoryInfo(@"C:\Test");
var destination = new DirectoryInfo(@"E:\Test");
source.CopyTo(destination);
Include this file in your project
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace System.IO
{
public static class DirectoryInfoExtensions
{
public static void CopyTo(this DirectoryInfo source, DirectoryInfo target)
{
if (!target.Exists)
target.Create();
foreach (var file in source.GetFiles())
file.CopyTo(Path.Combine(target.FullName, file.Name), true);
foreach (var subdir in source.GetDirectories())
subdir.CopyTo(target.CreateSubdirectory(subdir.Name));
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With