我想知道如何将目录从其父目录移动到其父目录。这听起来有点奇怪,所以我会想象一下。我们有文件夹a,B,C,C在B中,等等。我需要把文件夹C从B中移到目录a中。
我已经尝试过使用这个(extractPath是文件夹A,FolderB是,嗯,文件夹B(:
Directory.Move(Directory.GetDirectories(extractPath + @"FolderB")[0],Directory.GetParent(extractPath + @"FolderB").ToString());
但是,由于此错误,它似乎正在尝试移动它:System.IO.IOException:"文件"C:\Users\user\Documents\FolderA\FolderB\FolderC\ExampleFile.png"已存在。">
我应该使用Path.Combine而不是将两者相加,但我只需要它来工作,我稍后会解决这个问题。
谢谢你的帮助。
编辑:我重写了我的代码,使其更容易阅读,并可能降低我弄乱的几率
String newDir = Directory.GetDirectories(extractPath + @"FolderB")[0]; //Should return folder C
Directory.Move(newDir, extractPath); //Moves folder C to folder A.
``
我有
Directory.Move(newDir, extractPath);
你必须添加newDir的文件名,我的原始文件夹正在移动。你可以通过获得该文件夹的名称
Path.GetFileName(newDir);
然后你会有
Directory.Move(newDir, extractPath + Path.GetFileName(newDir));