2

我有一个使用 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;
4

1 回答 1

4

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.

于 2012-03-01T03:48:57.167 回答