Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleManage the wastebasket in Visual Basic .NET
DescriptionThis example shows how to manage the wastebasket in Visual Basic .NET.
Keywordswastebasket, recycle bin, delete, delete file, empty recycle bin, empty wastebasket, Visual Basic .NET, VB.NET
CategoriesFiles and Directories, Utilities
 
This example uses interop methods to define functions in DLLs so it imports the System.Runtime.InteropServices namespace.

It then declares the SHQueryRecycleBin and SHEmptyRecycleBin API functions, and the structures they need.

 
<StructLayout(LayoutKind.Sequential, Pack:=1)> _
Public Structure SHQUERYRBINFO
    Dim cbSize As Int32
    Dim i64Size As Long
    Dim i64NumItems As Long
End Structure

<DllImport("shell32.dll")> _
Private Shared Function SHQueryRecycleBin(ByVal pszRootPath _
    As String, ByRef ptSHQueryRBInfo As SHQUERYRBINFO) As _
    Int32
End Function

Private Enum RecycleFlags As Int32
    SHERB_NOCONFIRMATION = &H1
    SHERB_NOPROGRESSUI = &H2
    SHERB_NOSOUND = &H4
End Enum

<DllImport("shell32.dll")> _
Private Shared Function SHEmptyRecycleBin(ByVal hwnd As _
    IntPtr, ByVal pszRootPath As String, ByVal dwFlags As _
    RecycleFlags) As Int32
End Function
 
Every second a timer executes the following code. It uses the SHQueryRecycleBin function to see how many files are in the recycle bin and how much space they occupy.
 
Private Sub tmrCheckBin_Tick(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles tmrCheckBin.Tick
    Static had_error As Boolean = False
    If had_error Then Exit Sub

    Try
        Dim info As SHQUERYRBINFO
        info.cbSize = Len(info)
        SHQueryRecycleBin(Nothing, info)
        lblNumItems.Text = info.i64NumItems & " items (" & _
            info.i64Size & " bytes)"
    Catch ex As Exception
        had_error = True
        MessageBox.Show("Error getting wastebasket " & _
            "information" & vbCrLf & ex.Message, "Error", _
            MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Sub
 
When you click the Delete File button, the following code executes. It examines the form's radio buttons to see whether it should permanently delete the file or move it to the wastebasket. It examines the chkConfirmDelete check box to see if it should make the user confirm the deletion.

The code then calls My.Computer.FileSystem.DeleteFile to delete the file. (This part of the program is purely .NET. Too bad there aren't similar methods for checking the wastebasket's size and emptying it.)

 
Private Sub btnDelete_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnDelete.Click
    Dim opt_recycle As FileIO.RecycleOption
    If radDeletePermanently.Checked Then
        opt_recycle = FileIO.RecycleOption.DeletePermanently
    Else
        opt_recycle = FileIO.RecycleOption.SendToRecycleBin
    End If

    Dim opt_confirm As FileIO.UIOption
    If chkConfirmDelete.Checked Then
        opt_confirm = FileIO.UIOption.AllDialogs
    Else
        opt_confirm = FileIO.UIOption.OnlyErrorDialogs
    End If

    Try
        My.Computer.FileSystem.DeleteFile( _
            txtFile.Text, opt_confirm, opt_recycle)
    Catch ex As Exception
        MessageBox.Show("Error deleting file" & vbCrLf & _
            ex.Message, "Error", MessageBoxButtons.OK, _
            MessageBoxIcon.Error)
    End Try

    MakeDummyFile()
End Sub
 
When you click the Empty Wastebasket button, the following code executes. The code sets appropriate options to optionally display a progress bar, play the recycling sound, and ask the user for confirmation. It then calls the SHEmptyRecycleBin API function to empty the wastebasket.
 
Private Sub btnEmpty_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnEmpty.Click
    Dim options As RecycleFlags = 0

    If Not chkProgress.Checked Then options = options Or _
        RecycleFlags.SHERB_NOPROGRESSUI
    If Not chkPlaySound.Checked Then options = options Or _
        RecycleFlags.SHERB_NOSOUND
    If Not chkConfirmEmpty.Checked Then options = options _
        Or RecycleFlags.SHERB_NOCONFIRMATION

    Try
        SHEmptyRecycleBin(Nothing, Nothing, options)
    Catch ex As Exception
        MessageBox.Show("Error emptying wastebasket" & _
            vbCrLf & ex.Message, "Error", _
            MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Sub
 
Unfortunately the SHEmptyRecycleBin function seems to ignore some of these parameters and does play the recycling sound or display a progress dialog for me. If you figure out how to fix this, please let me know.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated