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

I need to copy a whole directory C:X to C:YX, and I need the sub-folders to be copied as well.
Is there any way to do it with the System.IO.FileDirectory namespaces ?
Thanks for all helpers!

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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;
        }
    }
}

Method 2

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);
    }
}

Method 3

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);
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x