Library code snippets

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

Comments

  1. 24 Aug 2009 at 22:38

    can this code be used to send entire folders to the recycle bin

  2. 09 May 2007 at 10:38

    .NET 2.0

     

    My

    .Computer.FileSystem.DeleteFile("Path", FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.SendToRecycleBin)
  3. 06 Oct 2005 at 06:54


    hai i am working in a vb.net project. can i able to select one or more picture boxes simultanously at run time. Pls help with a revelent code
    Thank You
    Soumya

  4. 01 Jan 1999 at 00:00

    This thread is for discussions of Send File to Recycle Bin in VB .NET.

Leave a comment

Sign in or Join us (it's free).

Walter Steed
AddThis

Related podcasts

  • xpert to Expert: Inside Concurrent Basic (CB)

    "Concurrent Basic extends Visual Basic with stylish asynchronous concurrency constructs derived from the join calculus. Our design advances earlier MSRC work on Polyphonic C#, Comega and the Joins Library. Unlike its C# based predecessors, CB adopts a simple event-like syntax familiar to VB progr...

Want to stay in touch with what's going on? Follow us on twitter!