将文件夹移动到同名文件夹

Moving Folder to same named Folder

我有一些文件夹,当我的程序是 运行 时我会检查它。 我将不需要的文件夹和 return 选中的文件夹删除到另一个目录。

我的问题是我总是遇到异常。我真的不知道为什么。 顺便说一句:当前表和新表具有相同的名称 : 翻译,因为它是德语:无法创建数据,因为它已经存在。 我的代码:

public void CreateCheckedStructure() { 

        List<string> checkedDirNew = Program.RemoveTempFolders(GetAllFromDir(Settings.Default.NewFolder));
        List<string> checkedDirCurrent = Program.RemoveTempFolders(GetAllFromDir(Settings.Default.CurrentFolder));

        foreach(string checkedNew in checkedDirNew){

            DirectoryInfo dirInfoNew = new DirectoryInfo(checkedNew);
            foreach (string checkedCurrent in checkedDirCurrent) {
                DirectoryInfo dirInfoCurrent = new DirectoryInfo(checkedCurrent);
                if(dirInfoNew.Name.Equals(dirInfoCurrent.Name)){
                    string checkedFoldersPath = Settings.Default.CheckedTables + "\" + dirInfoCurrent.Name;
                    Directory.CreateDirectory(checkedFoldersPath);
                    Directory.CreateDirectory(checkedFoldersPath+"\New");
                    Directory.CreateDirectory(checkedFoldersPath + "\Current");
                    dirInfoCurrent.MoveTo(checkedFoldersPath + "\Current");
                    dirInfoNew.MoveTo(checkedFoldersPath + "\New");
                    break;
                }
            }        
        }

    }

您正在创建 checkedFoldersPath+"\New",然后试图在其上复制另一个文件夹。发生错误是因为它已经存在。如果您尝试合并这两个文件夹,则必须将所有子文件和子目录(递归地)移动到新文件夹,然后删除原始文件夹。

看到这个答案:C# merge one directory with another