“移动不能跨卷工作"- 为什么?以及如何克服? [英] "Move will not work across volumes" - Why? And how to overcome?
问题描述
为什么当源文件和目标文件在不同的分区时 File.Move(sourceFileName, destFileName)
工作正常,但是 Directory.Move(sourceDirName, destDirName)
代码> 不?它抛出
Why is it that File.Move(sourceFileName, destFileName)
works fine when the source file and destination files are in different partitions, but Directory.Move(sourceDirName, destDirName)
don't? It throws
System.IO.IOException: "源和目标路径必须有同根.移动不能跨卷工作."
System.IO.IOException: "Source and destination path must have identical roots. Move will not work across volumes."
我什至尝试创建一个 DirectoryInfo
实例并使用 MoveTo(destDirName)
方法,但没有成功.
I even tried to create a DirectoryInfo
instance and use the MoveTo(destDirName)
method but without success.
我错过了什么吗?我真的必须自己实现移动"功能吗?(顺便说一下,我要移动的目录很大).
Am I missing something? Do I really have to implement a "move" functionality myself? (the directory I want to move is very large btw).
推荐答案
您还可以 p/invoke SHFileOperation
与 Windows 资源管理器用来移动目录的功能相同.它将根据需要执行真正的移动或递归复制然后删除.
You can also p/invoke SHFileOperation
which is the same function Windows Explorer uses to move directories around. It will either perform a true move or recursive-copy-then-delete, as appropriate.
它还可以显示与资源管理器相同的进度 UI,只需设置一个标志即可.
It can also show the same progress UI as explorer, just by setting a flag.
这篇关于“移动不能跨卷工作"- 为什么?以及如何克服?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!