Send a suggestion!

We're building a brand new version of the site, and we'd love to hear your ideas

Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 20,919 times

Related Categories

Send File to Recycle Bin in VB .NET

WolfenSteed

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

Comments