ASP.NET C # Copy Directory with subdirectories with System.IO

I need to copy the entire directory C: \ X to C: \ Y \ X, and I also need the subfolders that need to be copied.
Is there a way to do this with the System.IO.File \ Directory namespace?
Thanks to all the helpers!

+4
source share
3 answers

This class will copy or move the folder without recursive calls.
Methods use their own recursion stacks to avoid a StackOverflowException .

public static class CopyFolder { public static void CopyDirectory(string source, string target) { var stack = new Stack<Folders>(); stack.Push(new Folders(source, target)); while (stack.Count > 0) { var folders = stack.Pop(); Directory.CreateDirectory(folders.Target); foreach (var file in Directory.GetFiles(folders.Source, "*.*")) { string targetFile = Path.Combine(folders.Target, Path.GetFileName(file)); if (File.Exists(targetFile)) File.Delete(targetFile); File.Copy(file, targetFile); } foreach (var folder in Directory.GetDirectories(folders.Source)) { stack.Push(new Folders(folder, Path.Combine(folders.Target, Path.GetFileName(folder)))); } } } public static void MoveDirectory(string source, string target) { var stack = new Stack<Folders>(); stack.Push(new Folders(source, target)); while (stack.Count > 0) { var folders = stack.Pop(); Directory.CreateDirectory(folders.Target); foreach (var file in Directory.GetFiles(folders.Source, "*.*")) { string targetFile = Path.Combine(folders.Target, Path.GetFileName(file)); if (File.Exists(targetFile)) File.Delete(targetFile); File.Move(file, targetFile); } foreach (var folder in Directory.GetDirectories(folders.Source)) { stack.Push(new Folders(folder, Path.Combine(folders.Target, Path.GetFileName(folder)))); } } Directory.Delete(source, true); } public class Folders { public string Source { get; private set; } public string Target { get; private set; } public Folders(string source, string target) { Source = source; Target = target; } } } 
+8
source

This is copied from the xneurons blog .

 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); } } 
+3
source

You can use SearchOption.AllDirectories to recursively search folders down, you just need to create directories before copying ...

 // string source, destination; - folder paths int pathLen = source.Length; foreach (string dirPath in Directory.GetDirectories(source, "*", SearchOption.AllDirectories)) { string subPath = dirPath.SubString(pathLen); string newpath = Path.Combine(destination, subPath); Directory.CreateDirectory(newpath ); } foreach (string filePath in Directory.GetFiles(source, "*.*", SearchOption.AllDirectories)) { string subPath = filePath.SubString(pathLen); string newpath = Path.Combine(destination, subPath); File.Copy(filePath, newpath); } 
+2
source

Source: https://habr.com/ru/post/1309293/


All Articles