Using SHFileOperation
to copy files in Delphi.
This code demonstrates how to use SHFileOperation to copy a file, as seen in Windows Explorer. SHFileOperation provides the same functionality as the copy, cut, delete and rename capabilities of Windows Explorer, and more importantly enables the user to undo any of these operations across the Windows '95 environment. |
uses
Windows, SysUtils,
ShellAPI, FileCtrl;
procedure SHCopyFile(hWndOwner:
HWND; const SourceFile, TargetFile: string);
var Info: TSHFileOpStruct;
Aborted
: Bool;
begin
Aborted := False;
with Info
do
begin
Wnd
:= hWndOwner;
wFunc
:= FO_COPY;
{
From Microsoft's Help:
wFunc = Operation to perform. This member can be one of the following
values:
FO_COPY Copies the files specified by pFrom to the location specified
by pTo.
FO_DELETE Deletes the files specified by pFrom (pTo is ignored).
FO_MOVE Moves the files specified by pFrom to the location specified
by pTo.
FO_RENAME Renames the files specified by pFrom. }
pFrom
:= pChar(SourceFile);
pTo
:= pChar(TargetFile);
fFlags
:= 0;
fAnyOperationsAborted
:= Aborted;
end;
try
SHFileOperation(Info);
finally
if
Aborted then; { enact upon any user cancellations
}
end;
end;
hWndOwner can be Application.Handle,
the Form's handle or the result of GetDeskTopWindow.