Kopiowanie zawartości katalogu wykluczając jeden podfolder

0

Próbuję przerobić funkcję która tworzy kopię katalogu, na taką która stworzy kopię ale bez jednego podkatalogu.
Nie umiem sobie poradzić z wykluczeniem jednego z podkatalogu którego nazwa może być na sztywno zapisana w funkcji, folder ten znajduje się w głównym katalogu.
ps. Chyba nigdy nie zrozumiem rekurencji bo chyba tak to jest tu zrobione.

private static void directory_copy(string sourceDirName, string destDirName, bool copySubDirs)
        {
            // Get the subdirectories for the specified directory.            
            DirectoryInfo dir = new DirectoryInfo(sourceDirName);
            DirectoryInfo[] dirs = dir.GetDirectories();
            if (!dir.Exists)
            {
                throw new DirectoryNotFoundException(
                    "Source directory does not exist or could not be found: "
                    + sourceDirName);
            }
            // If the destination directory doesn't exist, create it.
            if (!Directory.Exists(destDirName))
            {
                Directory.CreateDirectory(destDirName);               
            }

            // Get the files in the directory and copy them to the new location.
            FileInfo[] files = dir.GetFiles();
            foreach (FileInfo file in files)
            {
                string temppath = Path.Combine(destDirName, file.Name);
                file.CopyTo(temppath, false);
            }
            // If copying subdirectories, copy them and their contents to new location.
            if (copySubDirs)
            {
                foreach (DirectoryInfo subdir in dirs)
                {
                    string temppath = Path.Combine(destDirName, subdir.Name);
                    directory_copy(subdir.FullName, temppath, copySubDirs);
                }
            }
        }
1

To może bez rekurencji?

u mnie działa

private static void DeepDirectoryCopy(string path, string targetDir)
{
    var dirInfo = new DirectoryInfo(path);

    if (!dirInfo.Exists)
        return;

    var kłełełe = new Queue<(DirectoryInfo DirInfo, string TargetDirectory)>();

    kłełełe.Enqueue((dirInfo, targetDir));

    while (kłełełe.Count > 0)
    {
        var current = kłełełe.Dequeue();

        var targetPath = current.TargetDirectory;

        Directory.CreateDirectory(targetPath);

        foreach (var file in current.DirInfo.EnumerateFiles())
        {
            file.CopyTo(Path.Combine(targetPath, file.Name));
        }

        foreach (var dir in current.DirInfo.EnumerateDirectories())
        {
            kłełełe.Enqueue((dir, Path.Combine(targetPath, dir.Name)));
        }
    }
}
DeepDirectoryCopy(@"C:\Tests\dupa", @"C:\Tests\dupa2");

1 użytkowników online, w tym zalogowanych: 0, gości: 1