Code sample for empty Recycle Bin in VB.Net


following is the code sample for deleting all contents of recycle bin in VB.Net.

Following Code sample requires one Button control named ‘btnRecycleBin’ on windows form.

Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Imports System.Management
 
Public Class Form1
    Inherits Form
    Public Sub New()
        InitializeComponent()
    End Sub
 
    Private Enum RecycleFlags As UInteger
        SHRB_NOCONFIRMATION = &H1
        SHRB_NOPROGRESSUI = &H2
        SHRB_NOSOUND = &H4
    End Enum
    <DllImport("Shell32.dll", CharSet:=CharSet.Unicode)> _
    Private Shared Function SHEmptyRecycleBin(ByVal hwnd As IntPtr, ByVal pszRootPath As String, ByVal dwFlags As RecycleFlags) As UInteger
    End Function
    Private Sub btnRecycleBin_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim result As DialogResult
        result = MessageBox.Show("Are you sure to delete all items in recycle bin", "Empty Recycle bin", MessageBoxButtons.YesNo)
        If result = DialogResult.Yes Then
            Try
                Dim IsSuccess As UInteger = SHEmptyRecycleBin(IntPtr.Zero, Nothing, RecycleFlags.SHRB_NOCONFIRMATION)
                MessageBox.Show("Empty the RecycleBin successsfully", "Empty the RecycleBin", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Catch ex As Exception
                MessageBox.Show("Empty the RecycleBin failed" & ex.Message, "Empty the RecycleBin", MessageBoxButtons.OK, MessageBoxIcon.[Stop])
                Application.[Exit]()
            End Try
        End If
    End Sub
End Class