使用c#复制文件夹和所有子文件夹和文件的最佳方法是什么?

我需要将文件夹从一个驱动器复制到可移动硬盘。 需要复制的文件夹中将包含许多子文件夹和文件。 输入将是源路径和目标路径。

喜欢..

源路径:“C:\ SourceFolder”

目标路径:“E:\”

复制完成后,我可以在我的E:驱动器中看到文件夹“SourceFolder”。

谢谢。

我觉得这就是。

public static void CopyFolder(DirectoryInfo source, DirectoryInfo target) { foreach (DirectoryInfo dir in source.GetDirectories()) CopyFolder(dir, target.CreateSubdirectory(dir.Name)); foreach (FileInfo file in source.GetFiles()) file.CopyTo(Path.Combine(target.FullName, file.Name)); } 

在Channel9找到了这个。 我自己没试过。

 public static class DirectoryInfoExtensions { // Copies all files from one directory to another. public static void CopyTo(this DirectoryInfo source, string destDirectory, bool recursive) { if (source == null) throw new ArgumentNullException("source"); if (destDirectory == null) throw new ArgumentNullException("destDirectory"); // If the source doesn't exist, we have to throw an exception. if (!source.Exists) throw new DirectoryNotFoundException( "Source directory not found: " + source.FullName); // Compile the target. DirectoryInfo target = new DirectoryInfo(destDirectory); // If the target doesn't exist, we create it. if (!target.Exists) target.Create(); // Get all files and copy them over. foreach (FileInfo file in source.GetFiles()) { file.CopyTo(Path.Combine(target.FullName, file.Name), true); } // Return if no recursive call is required. if (!recursive) return; // Do the same for all sub directories. foreach (DirectoryInfo directory in source.GetDirectories()) { CopyTo(directory, Path.Combine(target.FullName, directory.Name), recursive); } } } 

用法如下:

 var source = new DirectoryInfo(@"C:\users\chris\desktop"); source.CopyTo(@"C:\users\chris\desktop_backup", true); 

如何:复制,删除和移动文件和文件夹(C#编程指南)
http://msdn.microsoft.com/en-us/library/cc148994.aspx

如何:遍历目录树(C#编程指南)
http://msdn.microsoft.com/en-us/library/bb513869.aspx

  private static bool CopyDirectory(string SourcePath, string DestinationPath, bool overwriteexisting) { bool ret = true; try { SourcePath = SourcePath.EndsWith(@"\") ? SourcePath : SourcePath + @"\"; DestinationPath = DestinationPath.EndsWith(@"\") ? DestinationPath : DestinationPath + @"\"; if (Directory.Exists(SourcePath)) { if (Directory.Exists(DestinationPath) == false) Directory.CreateDirectory(DestinationPath); foreach (string fls in Directory.GetFiles(SourcePath)) { FileInfo flinfo = new FileInfo(fls); flinfo.CopyTo(DestinationPath + flinfo.Name, overwriteexisting); } foreach (string drs in Directory.GetDirectories(SourcePath)) { DirectoryInfo drinfo = new DirectoryInfo(drs); if (CopyDirectory(drs, DestinationPath + drinfo.Name, overwriteexisting) == false) ret = false; } Directory.CreateDirectory(DI_Target + "//Database"); } else { ret = false; } } catch (Exception ex) { ret = false; } return ret; } 

对于googlers:在纯win32 / C ++中,使用SHCreateDirectoryEx

 inline void EnsureDirExists(const std::wstring& fullDirPath) { HWND hwnd = NULL; const SECURITY_ATTRIBUTES *psa = NULL; int retval = SHCreateDirectoryEx(hwnd, fullDirPath.c_str(), psa); if (retval == ERROR_SUCCESS || retval == ERROR_FILE_EXISTS || retval == ERROR_ALREADY_EXISTS) return; //success throw boost::str(boost::wformat(L"Error accessing directory path: %1%; win32 error code: %2%") % fullDirPath % boost::lexical_cast(retval)); //TODO *djg* must do error handling here, this can fail for permissions and that sort of thing } 

为什么不使用像Robocopy这样的东西?

它有一个镜像选项,其中源的目录结构按原样复制到目的地。 有各种命令行选项。 可以节省您复制代码中function的工作量。

以下是对问题的不同看法:

 System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(@"XCOPY C:\folder D:\Backup\folder /i"); psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; psi.UseShellExecute = false; System.Diagnostics.Process copyFolders = System.Diagnostics.Process.Start(psi); copyFolders.WaitForExit();