Send File to Recycle Bin in VB .NET

This is the VB.NET version of "Send file to recycle bin"

Option Strict Off
Option Explicit On
Imports System.IO
Module Module1
   
  Private Structure SHFILEOPSTRUCT
    Dim hwnd As Integer
    Dim wFunc As Integer
    Dim pFrom As String
    Dim pTo As String
    Dim fFlags As Short
    Dim fAnyOperationsAborted As Boolean
    Dim hNameMappings As Integer
    Dim lpszProgressTitle As String
  End Structure
   
  Private Const FO_DELETE As Short = &H3s
  Private Const FOF_ALLOWUNDO As Short = &H40s
  Private Const FOF_NOCONFIRMATION As Short = &H10s
   
  Private Declare Function SHFileOperation Lib "shell32.dll" Alias _
    "SHFileOperationA" (ByRef lpFileOp As SHFILEOPSTRUCT) As Integer
   
  Public Function Recycle(ByRef sPath As String) As Integer
      Dim FileOp As SHFILEOPSTRUCT
      If Not File.Exists(sPath) Then
        MsgBox("Could not find " & sPath & "." & vbCrLf _
        & "Please verify the path.")
        Recycle = -1
        Exit Function
      End If
      With FileOp
        .wFunc = FO_DELETE
        .pFrom = sPath & vbNullChar
        .pTo = vbNullChar
        .fFlags = FOF_NOCONFIRMATION Or FOF_ALLOWUNDO
        .lpszProgressTitle = "Sending " & sPath & " to the Recycle Bin"
      End With
      Try
        SHFileOperation(FileOp)
      Catch ex As Exception
        MsgBox(ex.Message)
      End Try
      Recycle = 0
  End Function
End Module

You might also like...

Comments

Walter Steed

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Brevity is the soul of wit” - Shakespeare