Send files to the recycle bin (Views: 2141)
Problem/Question/Abstract: How to send files to the recycle bin Answer: Solve 1: unit Recycle; interface uses Windows, Messages, SysUtils, Classes, Controls, Forms, Dialogs, ShellAPI; function RecycleFile(FileToRecycle: TFilename): boolean; function RecycleFileEx(FileToRecycle: TFilename; Confirm: boolean): boolean; implementation function RecycleFile(FileToRecycle: TFilename): boolean; begin Result := RecycleFileEx(FileToRecycle, True); end; function RecycleFileEx(FileToRecycle: TFilename; Confirm: boolean): boolean; var Struct: TSHFileOpStruct; tmp: string; Resultval: integer; begin tmp := FileToRecycle + #0#0; Struct.wnd := 0; Struct.wFunc := FO_DELETE; Struct.pFrom := PChar(tmp); Struct.pTo := nil; Struct.fFlags := FOF_ALLOWUNDO; if not Confirm then Struct.fFlags := Struct.fFlags or FOF_NOCONFIRMATION; Struct.fAnyOperationsAborted := false; Struct.hNameMappings := nil; try Resultval := ShFileOperation(Struct); except on e: Exception do begin e.Message := 'Tried to recycle file:' + FileToRecycle + #13#10 + e.Message; raise; end; end; Result := (Resultval = 0); end; end. Solve 2: uses ShellApi; function DeleteFilesToRecycleBin(const APath: string): Boolean; var AStruct: TShFileOpStruct; begin if Length(APath) = 0 then Exit; AStruct.Wnd := 0; AStruct.wFunc := FO_DELETE; AStruct.pFrom := PChar(APath); AStruct.fFlags := FOF_ALLOWUNDO; Result := ShFileOperation(AStruct) <> 0; end; Solve 3: function RecycleFile(FileToRecycle: string): boolean; var Struct: TSHFileOpStruct; pFromc: PChar; Resultval: integer; begin if not FileExists(FileToRecycle) then begin RecycleFile := False; exit; end else begin pfromc := PChar(ExpandFileName(FileToRecycle) + #0#0); Struct.wnd := 0; Struct.wFunc := FO_DELETE; Struct.pFrom := pFromC; Struct.pTo := nil; Struct.fFlags := FOF_ALLOWUNDO; Struct.fAnyOperationsAborted := false; Struct.hNameMappings := nil; Resultval := ShFileOperation(Struct); RecycleFile := (Resultval = 0); end; end; |