Description
This example shows how to use the SHQueryRecycleBin() function from the Win 32 Application Programming Interface (API) to get both the number of items in the recycle bin and the total disc space taken by these items.
All controls on the Form1 are created at runtime: 1 Tbuttons and 2 TLabels. The number of items is displayed on Label1 and the disc space
on Label2.
Code
QueryRecycleBin.cpp
#include <vcl.h>
#include <shellapi.h>
#include "QueryRecycleBin.h"
#pragma hdrstop
//----------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
const int DX_SEPARATION = 50;
const int DY_SEPARATION = 25;
TButton* Button1;
TLabel* Label1;
TLabel* Label2;
//-----------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
Color = clBlue;
Width = 800;
Height = 300;
Position = poScreenCenter;
Button1 = new TButton(this);
Button1->Parent = this;
Button1->Width = 200;
Button1->Height = 100;
Button1->Top = 50;
Button1->Left = Width/2 - Button1->Width - DX_SEPARATION;
Button1->Caption = "Query Recycle Bin";
Button1->Font->Size = 10;
Button1->OnClick = Button1Click;
Label1 = new TLabel(this);
Label1->Parent = this;
Label1->Caption = "Number of Items";
Label1->Font->Size = Button1->Font->Size;
Label1->Width = 100;
Label1->Height = 20;
Label1->Top = Button1->Top + Button1->Height + DY_SEPARATION;
Label1->Left = Button1->Left;
Label2 = new TLabel(this);
Label2->Parent = this;
Label2->Caption = "Space (Bytes)";
Label2->Font->Size = Button1->Font->Size;
Label2->Width = Label1->Width;
Label2->Height = Label1->Height;
Label2->Top = Label1->Top + DY_SEPARATION;
Label2->Left = Button1->Left;
}
//-----------------------------------------------------------------------
void QueryRecycleBin(String& SItems, String& SSize)
{
SHQUERYRBINFO obj;
// pQueryInfo:
// Address of a SHQUERYRBINFO structure that receives the Recycle Bin
// information. The cbSize member of the structure must be set to the
// size of the structure before calling this API.
LPSHQUERYRBINFO pQueryInfo = &obj;
pQueryInfo->cbSize = sizeof(SHQUERYRBINFO);
// pPath:
// Address of a null-terminated string whose maximum length is MAX_PATH
// This string will contain the path of the root drive on which the Recycle
// Bin is located. This parameter can contain the address of a string
// formatted with the drive, folder, and subfolder names(C:\Windows\System...).
char Path[MAX_PATH] = "C:";
LPCTSTR pPath = Path;
// Return Value:
// Returns S_OK if successful, or an OLE-defined error value if
// unsuccessful.
HRESULT r = SHQueryRecycleBin(pPath, pQueryInfo); // Win32 API
if (r == S_OK)
{
SItems = String(pQueryInfo->i64NumItems) + " items";
SSize = String(pQueryInfo->i64Size) + " Bytes";
}
else
{
SItems = "Error: SHQueryRecycleBin() failed";
SSize = "";
}
}
//-----------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
AnsiString SItems, SSize;
QueryRecycleBin(SItems, SSize);
// Display the results
Label1->Caption = SItems;
Label2->Caption = SSize;
}
//-----------------------------------------------------------------------
QueryRecycleBin.h
#ifndef QueryRecycleBinH
#define QueryRecycleBinH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
void __fastcall Button1Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
Copyright © 1997-2002 Rodolfo A. Frino. All rights reserved.