英文:
Is there a better way to move files and directory up a folder?
问题
根据标题,有没有更好的方法将所有文件和目录移到上一级文件夹?
这是我的代码:
private static void MoveEverythingUpAFolder(string sourceDir)
{
foreach (var item in Directory.GetFiles(sourceDir))
{
Directory.Move(item, Path.Combine(Path.GetDirectoryName(item), "..", Path.GetFileName(item)));
}
foreach (var item in Directory.GetDirectories(sourceDir))
{
Directory.Move(item, Path.Combine(Directory.GetParent(item).FullName, "..", new DirectoryInfo(item).Name));
}
Directory.Delete(sourceDir);
}
如果仅使用Directory.Move(source, dest)
,会出现目标目录已存在的异常...
英文:
as per title, is there a better way to move all the files and directories up a folder?
This is my code now:
private static void MoveEverythingUpAFolder(string sourceDir)
{
foreach (var item in Directory.GetFiles(sourceDir))
{
Directory.Move(item, Path.Combine(Path.GetDirectoryName(item), "..", Path.GetFileName(item)));
}
foreach (var item in Directory.GetDirectories(sourceDir))
{
Directory.Move(item, Path.Combine(Directory.GetParent(item).FullName, "..", new DirectoryInfo(item).Name));
}
Directory.Delete(sourceDir);
}
If I only use Directory.Move(source, dest)
I get an exception saying that the destination directory already exists...
答案1
得分: 1
这可能发生在你的源目录中有一个与源目录名称匹配的子目录的情况下。
例如,你试图将 c:\foo\bar\*.*
移动到 c:\foo
。
然而,存在一个名为 c:\foo\bar\bar
的目录。
foo/bar/bar
不能移动到 foo
并变成 foo/bar
,因为就像错误消息所说的那样,foo/bar
已经存在。
你会遇到问题。或者可能会出现异常。
你可以首先将源目录重命名为在该目录中不重复的名称,执行移动操作,然后删除该目录。
baz
英文:
This could happen if you have a directory inside your source directory, that matches the source directory name.
For example, you're trying to move c:\foo\bar\*.*
to c:\foo
.
However, a directory exists named c:\foo\bar\bar
.
foo/bar/bar
cannot be moved to foo
and become foo/bar
, because foo/bar
exists, just like the errors says.
You would be FUBAR. Or perhaps experience an exception.
You could rename the source directory first to something NOT duplicated within that directory, perform your move operations, and then remove the directory.
baz
答案2
得分: 0
答案取决于是否已知某些约束条件。
Directory.Move(string, string)
方法的文档说明:
此方法使用由 destDirName 指定的名称创建一个新目录,并将 sourceDirName 的内容(包括文件和目录)移动到新创建的目标目录。然后删除 sourceDirName 目录。
如果尝试将目录移动到已经存在的目录中,将引发 IOException。
如果您有如下的目录结构,尝试将 a\b\c
移动到 a\b
将会因为 b
已经存在而失败。
b
在更改前仅包含 c
a───b───c─┬─d
│
└─f
但是如果您知道目录 b
将只包含目录 c
,那么可以执行以下步骤:
- 将
a\b\c
移动到a\c-temp
- 删除
b
(现在应该是空的) - 将
c-temp
重命名为b
更改后
a───b─┬─d
│
└─f
如果目录 b
可能包含其他文件夹和文件,如下所示,则查找并移动各个文件夹和文件可能是最佳选择。
b
在更改前可能包含其他文件/文件夹
a───b─┬─c─┬─d
│ │
└─e └─f
- 将
c
中的文件夹和文件移动到b
(要注意可能会出现与已经存在于b
中的文件/文件夹的名称冲突) - 删除
c
(现在应该是空的)
更改后
a───b─┐
│
├─e
│
├─d
│
└─f
英文:
The answer depends on whether certain things are known constraints or not.
The documentation for the Directory.Move(string, string)
method states:
> This method creates a new directory with the name specified by destDirName and moves the contents of sourceDirName, including files and directories, to the newly created destination directory. It then deletes the sourceDirName directory.
>
> If you try to move a directory to a directory that already exists, an IOException will occur.
If you have a directory structure like the following, attempting to move a\b\c
to a\b
will fail with an error because b
exists.
b
will only contain c
- before changes
a───b───c─┬─d
│
└─f
But if you know that directory b
will only ever contain directory c
then you can
- move
a\b\c
toa\c-temp
- delete
b
(which should now be empty) - rename
c-temp
tob
after
a───b─┬─d
│
└─f
If directory b
may contain other folders and files as in the following, then finding and moving the individual folders and files may be the best.
b
may contain other files/folders - before changes
a───b─┬─c─┬─d
│ │
└─e └─f
- move folders and files in
c
tob
(be aware there could be name collisions with files/folders already present inb
) - delete
c
(which should now be empty)
after
a───b─┐
│
├─e
│
├─d
│
└─f
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论