寻找移动文件夹,子文件夹,文件,如果文件已经存在,则忽略移动

时间:2019-06-17 15:02:07

标签: c# directory move

我正在尝试将文件夹从一个位置移动到另一位置。如果该文件夹已经存在于目标目录中,但是其中的文件不存在,它将移动文件。我不知道要移动的是子文件夹,如果子文件夹已经存在于目标目录中,那么我需要移动其中的文件。


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!

0 个答案:

没有答案