MoveFileWithProgress 抛出“系统无法将文件移动到不同的磁盘驱动器” –为什么?
我有:
[SuppressUnmanagedCodeSecurity]
[DllImport("Kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool MoveFileWithProgress(
string lpExistingFileName, string lpNewFileName,
CopyProgressRoutine lpProgressRoutine,
int dwFlags);
public enum MoveFileOptions
{
MOVEFILE_COPY_ALLOWED = 0x2
}
并用以下方式调用它:
if (!MoveFileWithProgress(source.FullName, destination.FullName, cpr, (int)options)) {
throw new IOException(new Win32Exception().Message);
}
其中: options
是 MoveFileOptions.MOVEFILE_COPY_ALLOWED
在硬盘驱动器中移动时它工作正常。但是,当我尝试移动到闪存驱动器时,我得到:系统无法将文件移动到不同的磁盘驱动器
。
为什么?
I have:
[SuppressUnmanagedCodeSecurity]
[DllImport("Kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool MoveFileWithProgress(
string lpExistingFileName, string lpNewFileName,
CopyProgressRoutine lpProgressRoutine,
int dwFlags);
public enum MoveFileOptions
{
MOVEFILE_COPY_ALLOWED = 0x2
}
And calling it with:
if (!MoveFileWithProgress(source.FullName, destination.FullName, cpr, (int)options)) {
throw new IOException(new Win32Exception().Message);
}
Where: options
is MoveFileOptions.MOVEFILE_COPY_ALLOWED
It works fine when moving in the hard drive. But when I try moving to a Flash-drive, I get: The system cannot move the file to a different disk drive
.
Why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的
DllImport
不正确。您的函数只有 4 个参数,但是 真实函数 有 5 个。大概发生的情况是MOVEFILE_COPY_ALLOWED
被传递给lpData
并且被忽略。 dwFlags 参数就是堆栈上的任何参数。修复你的 p/invoke 可能会解决问题。另外,
dwFlags
应该是无符号的。正确后,您需要决定将什么传递给
lpData
。由于您目前似乎没有使用它,所以这并不重要,IntPtr.Zero
似乎是显而易见的选择。Your
DllImport
is incorrect. Your function has only 4 parameters, but the real function has 5. Presumably what is happening is thatMOVEFILE_COPY_ALLOWED
is being passed tolpData
and is ignored. ThedwFlags
parameter is just whatever happens to be sitting on the stack.Fixing your p/invoke will probably solve the problem. Also,
dwFlags
should be unsigned.With this correct you need to decide what to pass to
lpData
. Since you appear not to be using it at the moment, it doesn't really matter andIntPtr.Zero
seems the obvious choice.从这个 Microsoft | Technet 页面上写着:
在移动文件之前尝试重命名该文件。
From this Microsoft | Technet page, it says:
Try renaming the file before moving it.
您可能要移动目录吗?
根据 MSDN 上的 MoveFileWithProgress 文档(强调):
Are you perhaps moving a directory?
According to the documentation for MoveFileWithProgress at MSDN (emphasis added):