Question How to copy also subfolders and their files from an certain folder?

ken76

Member
Joined
Nov 15, 2018
Messages
23
Programming Experience
5-10
Can someone help how I can also copy user's desktop folder's subfolders and their files with the code below this text?
For moment I can only copy files from " + computer + "\c$\Users\Desktop folder but not the subfolders and their files.
C#:
   [B][I]string[] dirs = Directory.GetDirectories("[/I][/B][B][I]\\\\[/I][/B][B][I]" + computer + "[/I][/B][URL="file://c$//Users//"][B][I]\\c$\\Users\\[/I][/B][/URL][B][I]");[/I][/B]
[B][I]    foreach (string item in dirs)[/I][/B]
[B][I]    {[/I][/B]
[B][I]        FileInfo f = new FileInfo(item);[/I][/B]
[B][I]         user2 = "[/I][/B][B][I]\\\\[/I][/B][B][I]" + computer + "[/I][/B][URL="file://c$//Users//"][B][I]\\c$\\Users\\[/I][/B][/URL][B][I]" + f.Name + "[/I][/B][URL="file://desktop/"][B][I]\\Desktop[/I][/B][/URL][B][I]";[/I][/B]
[B][I]         if (Directory.Exists(user2))[/I][/B]
[B][I]                            {[/I][/B]
[B][I]         user = "C:\\Backup" + computer + "" + f.Name + "[/I][/B][URL="file://desktop/"][B][I]\\Desktop[/I][/B][/URL][B][I]";[/I][/B]
[B][I]         Directory.CreateDirectory(user);[/I][/B]
[B][I]         DirectoryInfo folder = new DirectoryInfo(user2);[/I][/B]
[B][I]         FileInfo[] files = folder.GetFiles();[/I][/B]
[B][I]         foreach (FileInfo file in files)[/I][/B]
[B][I]         {[/I][/B]
[B][I]               string temppath = Path.Combine(user, file.Name);[/I][/B]
[B][I]                file.CopyTo(temppath, true);[/I][/B]
[B][I]          }[/I][/B]
[B][I]     }[/I][/B]
 
There are two options:

1. Write a recursive method and do one folder at a time, e.g.
private void CopyFolder(string sourceFolderPath, string destinationFolderPath)
{
    if (!Directory.Exists(destinationFolderPath))
    {
        Directory.CreateDirectory(destinationFolderPath);
    }

    foreach (var sourceFilePath in Directory.GetFiles(sourceFolderPath))
    {
        var destinationFilePath = Path.Combine(destinationFolderPath, Path.GetFileName(sourceFilePath));

        File.Copy(sourceFilePath, destinationFilePath, true);
    }

    foreach (var sourceSubfolderPath in Directory.GetDirectories(sourceFolderPath))
    {
        // NOTE: Path.GetFile justgets the leaf node name so it can be used to get a folder name too.
        var destinationSubfolderPath = Path.Combine(destinationFolderPath, Path.GetFileName(sourceSubfolderPath));

        CopyFolder(sourceSubfolderPath, destinationSubfolderPath);
    }
}

2. Get all the file paths recursively first and then copy them one by one, e.g.
private void CopyFolder(string sourceFolderPath, string destinationFolderPath)
{
    var filePaths = Directory.GetFiles(sourceFolderPath, "*", SearchOption.AllDirectories);

    foreach (var sourceFilePath in filePaths)
    {
        var destinationFilePath = sourceFilePath.Replace(sourceFolderPath, destinationFolderPath);
        var destinationFileFolderPath = Path.GetDirectoryName(destinationFilePath);

        if (!Directory.Exists(destinationFileFolderPath))
        {
            Directory.CreateDirectory(destinationFileFolderPath);
        }

        File.Copy(sourceFilePath, destinationFilePath, true);
    }
}
 
Can it be done with your code?

Why are you asking me that instead of trying it for yourself? If it works, that's your answer. If it doesn't work, you post back and tell us exactly what you did and exactly what happened when you did it. You don't ask us to tell you things that you can easily find out for yourself.
 
Back
Top Bottom