|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Visual Basic System Services SHQueryRecycleBin: Recycle Bin Management |
||
Posted: | Tuesday January 13, 1997 | |
Updated: | Monday December 26, 2011 | |
Applies to: | VB4-32, VB5, VB6 | |
Developed with: | VB4-32, Windows 95 | |
OS restrictions: | See below | |
Author: | VBnet - Randy Birch | |
Related: |
SHFileOperation: Copy or Move an Entire Directory SHFileOperation: Add Recycle Bin Functionality SHFileOperation: Copy, Move or Delete Files |
|
Prerequisites |
NT: Requires version 5.0 or later (or version 4.0 with
Internet Explorer 4.0 or later). Windows: Requires Windows 98 (or Windows 95 with Internet Explorer 4.0 or later). |
|
Many
have written asking about Recycle Bin routines. Here, the SHQueryRecycleBin API is used to determine the contents of the bins, and the
SHEmptyRecycleBin API dumps the contents, based on flags set by the user.
In addition, the code demonstrates using the ShellExecute API to open Explorer at the C:\ folder in 'explore' mode (both treeview and listview displayed), and to open the Recycle Bin with the 'open' verb (listview only), using the Recycle Bin's registry CLSID. Start a new project, and copy the following code as indicated. New: On Windows 2000/XP, vbNullString is not a valid drive parameter. On this OS, you must pass the root drive of the bin of interest, ie "c:", "d:", etc. This is true regardless of the recycle bin's "Use one setting for all drives" setting. This change in behaviour means that the bin count received on querying the recycle bin will reflect only the contents of recycle bin on the drive indicated. |
BAS Module Code |
None: |
|
Form Code |
To a form, add 3 check buttons in a control array (Check1(0) - Check1(2)) with the captions as shown in the illustration. Set their initial values to 0. Add 5 command buttons (Command1 - Command5), and a label (Label1). |
|
Option Explicit '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved. ' Some pages may also contain other copyrights by the author. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Distribution: You can freely use this code in your own ' applications, but you may not reproduce ' or publish this code on any web site, ' online service, or distribute as source ' on any media without express permission. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Private Type myLong64 var1 As Long var2 As Long End Type Private Type SHQUERYRBINFO cbSize As Long i64Size As myLong64 i64NumItems As myLong64 End Type Private Const SW_NORMAL = 1 Private Const SW_SHOWMAXIMIZED = 3 Private Const SW_SHOWDEFAULT = 10 Private Const SW_SHOWNOACTIVATE = 4 Private Const SW_SHOWNORMAL = 1 Private Const HWND_DESKTOP = 0 Private Const SHERB_NOCONFIRMATION = &H1 Private Const SHERB_NOPROGRESSUI = &H2 Private Const SHERB_NOSOUND = &H4 Private Declare Function ShellExecute Lib "shell32" _ Alias "ShellExecuteA" _ (ByVal hwnd As Long, _ ByVal lpOperation As String, _ ByVal lpFile As String, _ ByVal lpParameters As String, _ ByVal lpDirectory As String, _ ByVal nShowCmd As Long) As Long Private Declare Function SHQueryRecycleBin Lib "shell32" _ Alias "SHQueryRecycleBinA" _ (ByVal pszRootPath As String, _ pSHQueryRBInfo As SHQUERYRBINFO) As Long Private Declare Function SHEmptyRecycleBin Lib "shell32" _ Alias "SHEmptyRecycleBinA" _ (ByVal hwnd As Long, _ ByVal pszRootPath As String, _ ByVal dwFlags As Long) As Long Private Sub Form_Load Command1.Caption = "Open Recycle Bin" Command2.Caption = "Explore C" Command3.Caption = "Requery Bin Status" Command4.Caption = "Empty Recycle Bin" Command5.Caption = "Exit" Check1(0).Caption = "Don't show confirmation dialog" Check1(1).Caption = "Don't show progress dialog" Check1(2).Caption = "Don't play completion sound" 'get the bin stats by setting 'the value property of the 'requery button to true Command3.Value = True End Sub Private Sub Command1_Click() Dim sCommand As String 'Win95 class ID of the Recycle Bin sCommand = "/root,::{645FF040-5081-101B-9F08-00AA002F954E}" Call ShellExecute(HWND_DESKTOP, _ "Open", _ "explorer.exe", _ sCommand, 0&, _ SW_SHOWNORMAL) End Sub Private Sub Command2_Click() Dim sCommand As String 'Win95 class ID of the Recycle Bin sCommand = "/e,c:\" Call ShellExecute(HWND_DESKTOP, _ "Open", _ "explorer.exe", _ sCommand, 0&, _ SW_SHOWNORMAL) End Sub Private Sub Command3_Click() Dim binCount As Long binCount = GetBitBucketCount() If binCount Then Label1.Caption = "The Recycle Bin contains " & binCount & " items." Else Label1.Caption = "The Recycle Bin is empty." End If Command4.Enabled = binCount > 0 End Sub Private Sub Command4_Click() EmptyBitBucket End Sub Private Sub Command5_Click() Unload Me End Sub Private Function EmptyBitBucket(Optional drvBin As String) Dim flags As Long 'if a specific bin is nor passed, 'then set the drvBin to a null string 'to perform the action on all bins If drvBin = "" Then drvBin = vbNullString End If 'set the option flags based on the 'check buttons If Check1(0).Value = 1 Then flags = SHERB_NOCONFIRMATION If Check1(1).Value = 1 Then flags = flags Or SHERB_NOPROGRESSUI If Check1(2).Value = 1 Then flags = flags Or SHERB_NOSOUND 'empty the bin Call SHEmptyRecycleBin(Me.hwnd, drvBin, flags) DoEvents 'update the stats by setting 'the value property of the 'requery button to true Command3.Value = True End Function Private Function GetBitBucketCount() As Long Dim SHQBI As SHQUERYRBINFO 'just need to set the length of the structure With SHQBI .cbSize = LenB(SHQBI) End With Call SHQueryRecycleBin(vbNullString, SHQBI) GetBitBucketCount = (SHQBI.i64NumItems.var1 + SHQBI.i64NumItems.var2) End Function |
Comments |
Save & run the project. Pressing the Open Recycle Bin
button will display the bin as though the icon was double clicked. Pressing Explore C: opens a normal explorer window rooted at C:\.
The Requery Bin Status button is used to update the label and to activate the Empty Recycle Bin button when disabled. The button will only enable when the bin has something to delete. Selecting the first checkbox will turn off the "Do you want to delete" prompt; the bin will empty without confirmation. For deleting a large number of files, the second checkbox prevents the flying garbage AVI progress dialog. The third option suspends the confirmation sound if set up on your system. |
|
|
|
|||||
|
|||||
|
|||||
Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved. |