1. Home
  2. Computing & Technology
  3. Delphi Programming

How to Programmatically Open the Recycle Bin

By Zarko Gajic, About.com

When you delete a file in Windows Explorer or My Computer, the file appears in the Recycle Bin. The file remains in the Recycle Bin until you empty the Recycle Bin or restore the file.

Here's how to programmatically display the Recycle Bin to a user of your Delphi application.

uses
  ShlObj, ShellAPI, ... ;

Place a TButton named "OpenBinButton" on a form, handle its OnClick event as:

procedure TRecycleBinForm.OpenBinButtonClick(Sender: TObject) ;
var
   recycleBinPIDL: PItemIDList;
   execInfo: TShellExecuteInfo;
begin
   SHGetSpecialFolderLocation(Handle, CSIDL_BITBUCKET, recycleBinPIDL) ;
   with execInfo do
   begin
     cbSize := Sizeof(execInfo) ;
     fMask := SEE_MASK_IDLIST;
     Wnd := Handle;
     lpVerb := nil;
     lpFile := nil;
     lpParameters := nil;
     lpDirectory := nil;
     nShow := SW_SHOWNORMAL;
     hInstApp:=0;
     lpIDList := recycleBinPIDL;
   end;
   ShellExecuteEx(@execInfo) ;
end;

Note: The code uses the SHGetSpecialFolderLocation Shell API function to get the location of the Recycle Bin, as a special folder (by passing the CSIDL_BITBUCKET value).

ShellExecuteEx is used to open the Recycle Bin's "folder".

Related: Delete files with the ability to UNDO (delete to Recycle Bin), Empty Recycle Bin (from Delphi code, of course).

Delphi tips navigator:
» How to Jump to the Last Item in the TListBox
« Activate a Running Instance when a User Opens an Associated File (By Extension)

More Delphi Programming Quick Tips
Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. Delphi 2006 Tips
  7. How to Programmatically Open the Recycle Bin from Delphi Code

©2009 About.com, a part of The New York Times Company.

All rights reserved.