This program shows you how to empty the Recycle Bin through a program.
Add a TButton component to the form that activates the emptying of the Recycle Bin. The design-finished main interface is shown in Figure 1.
Figure 1 Main interface
First, you define 3 constants and a type declaration in the implementation section of your program, and the code is as follows:
const
SHERB_NOCONFIRMATION = $00000001 ;
SHERB_NOPROGRESSUI = $00000002 ;
SHERB_NOSOUND = $00000004 ;
type
TSHEmptyRecycleBin=function(Wnd:HWND;LPCTSTR:PChar;DWORD:Word):Integer;stdcall;
At the beginning of the program, the program will first load the dynamic link library Shell32.dll through the LoadLibrary function, and store the loaded operation handle in the Libhandle. The address of the Shemptyrecyclebina function is then read through the @shemptyrecyclebin:=getprocaddress (Libhandle, ' Shemptyrecyclebina ') statement if the above statement executes correctly (That is, @shemptyrecyclebin is not equal to nil), then call Shemptyrecyclebin to complete the empty Recycle Bin operation. Finally, release the dynamic link library. The response code is as follows:
procedure TForm1.Button1Click(Sender: TObject);
var
SHEmptyRecycleBin:TSHEmptyRecycleBin;
LibHandle:THandle;
begin
LibHandle := LoadLibrary(PChar('Shell32.dll')) ;
if LibHandle <> 0 then
begin
@SHEmptyRecycleBin := GetProcAddress(LibHandle, 'SHEmptyRecycleBinA');
end
else
begin
MessageDlg('Failed to load Shell32.dll.', mtError, [mbOK], 0);
Exit ;
end ;
if @SHEmptyRecycleBin <> nil then
begin
SHEmptyRecycleBin(Application.Handle,'',SHERB_NOCONFIRMATION or SHERB_NOPROGRESSUI or SHERB_NOSOUND) ;
end;
FreeLibrary(LibHandle);
@SHEmptyRecycleBin := nil ;
end;