如果请求 FOF_NOERRORUI,则使用 SHFileOperation 复制目录结构失败
我有一个使用 SHFileOperation 将一个目录复制到另一个目录的工作代码。在本例中,这是 Pascal 代码,但我也在 C++ 中使用了相同的函数,并且问题似乎与 Windows 核心有关,而不是特定的编程语言。
根据 MSDN,我想指定以下标志组合:
FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_NOERRORUI
也就是说,我不需要进度条,我通过隐含的“是”答案抑制有关文件和目录的所有可能的问题,并且我不想要任何错误消息在 GUI(对话框)中。
使用此标志组合,该函数将返回错误 0x4C7(已被用户取消,这不是真的)。如果我删除 FOF_NOERRORUI 它可以在相同的输入参数和文件系统状态下正常工作。
不幸的是,我还需要抑制错误消息,并且需要 FOF_NOERRORUI 标志。
有人知道应该如何调整这种标志组合(可能是其他先决条件)以满足我的需求?
对于那些可能认为存在一些错误的人来说,这是源代码:
function CopyDirectory(WindowHandle: HWND; FilenameFrom: string; FilenameTo: string): Boolean;
var
SH: TSHFILEOPSTRUCT;
begin
FillChar(SH, SizeOf(SH), 0);
with SH do
begin
Wnd := WindowHandle;
wFunc := FO_COPY;
pFrom := PChar(FilenameFrom + #0);
pTo := PChar(FilenameTo + #0);
fFlags := FOF_NOCONFIRMMKDIR or FOF_NOCONFIRMATION or FOF_SILENT or FOF_NOERRORUI;
end;
Result := SHFileOperation(SH) = 0;
Result := Result and (not SH.fAnyOperationsAborted);
end;
I have a working code utilizing SHFileOperation for copying one directory into another. In this case this is Pascal code, but I also have been using the same function in C++, and the problem seems related to Windows core, not a specific programming language.
According to MSDN, I want to specify the following combination of flags:
FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_NOERRORUI
That is, I do not need a progress bar, I suppress all possible questions about files and directories by implied 'yes' answers, and I don't want any error message in GUI (dialog boxes).
With this combination of flags, the function returns error 0x4C7 (cancelled by user, which is not true). If I remove the FOF_NOERRORUI it works ok on the same input parameters and filesystem state.
Unfortunately, I need to suppress error messages as well, and FOF_NOERRORUI flag is required.
Does someone know how this combination of flags (and may be other prerequisites) should be adjusted to meet my needs?
Here is the source code for those who may think some bugs are there:
function CopyDirectory(WindowHandle: HWND; FilenameFrom: string; FilenameTo: string): Boolean;
var
SH: TSHFILEOPSTRUCT;
begin
FillChar(SH, SizeOf(SH), 0);
with SH do
begin
Wnd := WindowHandle;
wFunc := FO_COPY;
pFrom := PChar(FilenameFrom + #0);
pTo := PChar(FilenameTo + #0);
fFlags := FOF_NOCONFIRMMKDIR or FOF_NOCONFIRMATION or FOF_SILENT or FOF_NOERRORUI;
end;
Result := SHFileOperation(SH) = 0;
Result := Result and (not SH.fAnyOperationsAborted);
end;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
0x4C7实际上是:
如果您关闭所有标志并让操作运行,您会问什么类型的问题?我的猜测是,其中一个问题的答案是“否”,因为安全的选择是这样做。
更新
您是否考虑过使用
CopyFile()
API函数?无需 UI 抑制。文档位于此处 。0x4C7 is actually:
If you turn off all the flags and let the operation run, what sort of questions are you asked? My guess is that one of those questions is being answered as "No" because the safe option is to do that.
Update
Have you thought of using the
CopyFile()
API function? No UI suppression necessary. The documentation is here.