如果请求 FOF_NOERRORUI,则使用 SHFileOperation 复制目录结构失败

发布于 2025-01-03 05:53:31 字数 1049 浏览 3 评论 0原文

我有一个使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

GRAY°灰色天空 2025-01-10 05:53:31

0x4C7实际上是:

“操作已被用户取消,或者如果向 SHFileOperation 提供了适当的标志,则静默取消。”

如果您关闭所有标志并让操作运行,您会问什么类型的问题?我的猜测是,其中一个问题的答案是“否”,因为安全的选择是这样做。

更新

您是否考虑过使用CopyFile() API函数?无需 UI 抑制。文档位于此处

0x4C7 is actually:

"The operation was canceled by the user, or silently canceled if the appropriate flags were supplied to SHFileOperation."

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文