uses COMObj,shlobj,
ActiveX;
type
PPIDLItem = ^TPIDLItem;
TPIDLItem =
record
Dateiname :
string;
IDL : PItemIDList;
end;
function PapierkorbDateiWiederherstellen(Dateiname:
string):Boolean;
var
DeskDirI, RecycleI: IShellFolder;
pReIDL, pItemIDL: PItemIDList;
CmInfo: CMINVOKECOMMANDINFO;
ContextI: IContextMenu;
PIDLListe:TList;
PPIDL:PPIDLItem;
i:integer;
//------------------------------------------------------------------------------------------------------------------------------------------
procedure PapierkorbDateienAuflisten;
var
DeskDirI, RecycleI: IShellFolder;
pReIDL, pNextIDL: PItemIDList;
EnumList: IENUMIDLIST;
IsThere: Cardinal;
StrRet: TStrRet;
parName:
String;
PPIDL:PPIDLItem;
begin
OleCheck(SHGetDesktopFolder(DeskDirI));
OleCheck(SHGetSpecialFolderLocation(Application.Handle, CSIDL_BITBUCKET, pReIDL));
OleCheck(DeskDirI.BindToObject(pReIDL,
nil, IShellFolder, RecycleI));
CoTaskMemFree(pReIDL);
OleCheck(RecycleI.EnumObjects(Application.Handle, SHCONTF_FOLDERS
or SHCONTF_NONFOLDERS
or SHCONTF_INCLUDEHIDDEN, EnumList));
while EnumList.Next(1, pNextIDL, IsThere) = S_OK
do begin
If IsThere > 0
then begin
try
OleCheck(RecycleI.GetDisplayNameOf(pNextIDL, SHGDN_NORMAL, StrRet));
except
CoTaskMemFree(pNextIDL);
// Be sure to Free the memory
end;
case StrRet.uType
of
STRRET_CSTR: parName := StrRet.cStr;
STRRET_OFFSET: parName := PChar(Cardinal(pNextIDL)+StrRet.uOffset);
STRRET_WSTR: parName := StrRet.pOleStr;
end;
end;
If pNextIDL <>
nil then begin
New(PPIDL);
PPIDL^.Dateiname := parName;
PPIDL^.IDL := pNextIDL;
PIDLListe.Add(PPIDL);
end;
end;
end;
//------------------------------------------------------------------------------------------------------------------------------------------
procedure LeerePIDLListe;
begin
While PIDLListe.Count > 0
do begin
PPIDL := PPIDLItem(PIDLListe[0]);
PPIDL^.Dateiname := '
';
PIDLListe.Delete(0);
Dispose(PPIDL);
end;
FreeAndNil(PIDLListe);
end;
//------------------------------------------------------------------------------------------------------------------------------------------
begin
Result := False;
PIDLListe := TList.Create;
Try
OleCheck(SHGetDesktopFolder(DeskDirI));
OleCheck(SHGetSpecialFolderLocation(Application.Handle, CSIDL_BITBUCKET, pReIDL));
OleCheck(DeskDirI.BindToObject(pReIDL,
nil, IShellFolder, RecycleI));
// get Recycle shell folder
CoTaskMemFree(pReIDL);
// code above gets the Recycle Bin IShellFoldr Interface in RecycleI
PapierkorbDateienAuflisten;
For i := 0
to PIDLListe.Count - 1
do begin
If SameText(PPIDLItem(PIDLListe[i])^.Dateiname,Dateiname)
then begin
pItemIDL := PItemIDList(PPIDLItem(PIDLListe[i])^.IDL);
break;
end;
If i = PIDLListe.Count - 1
then begin
LeerePIDLListe;
exit;
end;
end;
Except
LeerePIDLListe;
exit;
End;
If pItemIDL <>
nil then begin
try
ZeroMemory(@CmInfo, SizeOf(CmInfo));
with CmInfo
do begin
cbSize:= SizeOf(CmInfo);
fMask:= CMIC_MASK_FLAG_NO_UI;
// this fmask of NO_UI is suppose to NOT show the "Do You want to?" dialog box
hwnd:= Application.Handle;
lpVerb:= '
undelete';
// restore the recycle bin item
nShow:= SW_SHOWDEFAULT;
end;
OleCheck(RecycleI.GetUIObjectOf(Application.Handle, 1, pItemIDL, IID_IContextMenu,
nil, ContextI));
OleCheck(ContextI.InvokeCommand(CmInfo));
Except
CoTaskMemFree(pItemIDL);
LeerePIDLListe;
exit;
end;
end;
LeerePIDLListe;
// Result := True;
Result := FileExists(Dateiname);
end;