How to Delete Files with the Ability to Recycle

If you want to delete a file and have the ability to recycle it you should better forget about low-level functions such as DeleteFile(). You need to use the Shell API functions in order to use the recycle bin. This is because deleting files with the use of the recycle bin is not just moving it there =)

Here is the code you should use:

uses ShellAPI;
  ...
function RecycleFile(sFileName: string): Boolean;
var
  FOS: TSHFileOpStruct;
begin
  FillChar(FOS, SizeOf(FOS), 0);
  with FOS do begin
    wFunc := FO_DELETE;
    pFrom := PChar(sFileName);
    fFlags := FOF_ALLOWUNDO or FOF_NOCONFIRMATION or FOF_SILENT;
  end;
  Result := (SHFileOperation(FOS) = 0);
end;

Tags: , , , , , , , , , , , , ,

No Comments Yet

You can be the first to comment!

Leave a comment

OpenID Login

Standard Login