[英]Moving a sub-directory into the parent of the main directory in C#
I'm looking to figure out how to move a directory out of its parent, into its parents, parent.我想弄清楚如何将目录从其父目录移出到其父目录中。 It sounds a little odd, so I'll visualize it.
这听起来有点奇怪,所以我会形象化它。 We have Folder A, B, C, C Being in B, etc. I need to move folder C out of B, and into directory A.
我们有文件夹 A、B、C、C 在 B 等中。我需要将文件夹 C 从 B 移到目录 A 中。
I've already attempted it using this (extractPath is folder A, FolderB is, well folder B):我已经尝试过使用它(extractPath 是文件夹 A,FolderB 是文件夹 B):
Directory.Move(Directory.GetDirectories(extractPath + @"\FolderB")[0],Directory.GetParent(extractPath + @"\FolderB").ToString());
However from this error, it appears to be trying to move it: System.IO.IOException: 'The file 'C:\Users\user\Documents\FolderA\FolderB\FolderC\ExampleFile.png' already exists.'但是,从这个错误来看,它似乎在尝试移动它:System.IO.IOException: 'The file 'C:\Users\user\Documents\FolderA\FolderB\FolderC\ExampleFile.png' already exists.'
I should be using Path.Combine rather than adding the two, but I just need it to work, I'll fix that later.我应该使用 Path.Combine 而不是将两者相加,但我只需要它工作,我稍后会修复它。
Thanks for your help.谢谢你的帮助。
Edit: I rewrote my code to be easier to read, and probably lower the chance of me messing something up编辑:我重写了我的代码以便于阅读,并可能降低我搞砸的机会
String newDir = Directory.GetDirectories(extractPath + @"\FolderB")[0]; //Should return folder C
Directory.Move(newDir, extractPath); //Moves folder C to folder A.
``
I fixed it: It was a combination of a few minor details but the main issue is this:我修好了:这是一些小细节的组合,但主要问题是:
I had我有
Directory.Move(newDir, extractPath);
You've got to add the name of the file of newDir, my original folder im moving.您必须添加 newDir 文件的名称,我正在移动的原始文件夹。 You can get the name of just that folder with
您可以获得该文件夹的名称
Path.GetFileName(newDir);
You will then have你将拥有
Directory.Move(newDir, extractPath + Path.GetFileName(newDir));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.