Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 namespaces ?
Thanks for all helpers!

like image 235
geevee Avatar asked Dec 12 '22 22:12

geevee


2 Answers

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;
        }
    }
}
like image 89
Jens Granlund Avatar answered Feb 22 '23 03:02

Jens Granlund


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);
    }
}
like image 36
Flexo Avatar answered Feb 22 '23 03:02

Flexo