Нужно переместить папку, подпапки, файлы и игнорировать перемещение, если файл уже существует - PullRequest
0 голосов
/ 17 июня 2019

Я пытаюсь переместить папки из одного места в другое.Если папка уже существует в целевой директории, а файлы внутри нее - нет, она перемещает файлы.Что я не могу понять, так это переместить подпапки, и если подпапка уже существует в целевой директории, то мне нужно переместить файлы в ней.


else if (Directory.Exists(source) && Directory.Exists(target))
{
     string[] sourcefiles = Directory.GetFiles(source);

     foreach (string sourcefile in sourcefiles)
     {
        string fileName = Path.GetFileName(sourcefile);
        string destFile = Path.Combine(target, fileName);

        if (!File.Exists(destFile)) 
        {
            File.Move(sourcefile, destFile);            
        }

        DirectoryInfo dir = new DirectoryInfo(source);
        DirectoryInfo[] dirs = dir.GetDirectories();

        foreach (DirectoryInfo subdir in dirs)
        {
           string temppath = Path.Combine(target, subdir.Name);

           if (Directory.Exists(temppath))
           {
                //maybe another loop here to check if files exist in 
                subfolders??
           }
           else
           {
             Directory.Move(subdir.FullName, temppath);
           }
         }
}

It's only moving files but not subfolders.  I need this to also move subfolders.  Thanks!
...