将子目录移动到C#中主目录的父目录中

  • 本文关键字:主目录 移动 子目录 c# file
  • 更新时间 :
  • 英文 :


我想知道如何将目录从其父目录移动到其父目录。这听起来有点奇怪,所以我会想象一下。我们有文件夹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));

最新更新