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

前端 未结 3 669
醉酒成梦
醉酒成梦 2021-01-14 01:27

I need to copy a whole directory C:\\X to C:\\Y\\X, and I need the sub-folders to be copied as well.
Is there any way to do it with the System.IO.File\\Directory namespa

相关标签:
3条回答
  • 2021-01-14 01:45

    This is copied from 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);
        }
    }
    
    0 讨论(0)
  • 2021-01-14 02:08

    This class will copy or move a folder, without recursive calls.
    The methods is using their own stacks to handle recursion, this is to avoid 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;
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-14 02:11

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

    // 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);
    }
    
    0 讨论(0)
提交回复
热议问题