Per permettere agli utenti dei vostri programmi di spostare un file nel Cestino di Windows (alias “Recycle Bin”) dobbiamo utilizzare una funzione della libreria di sistema shell32.dll.
Per rendere più semplici le cose, potete utilizzare il seguente modulo di codice:
Imports System.IO
Module SpostaFileCestino
Private Structure ShFileOpStruct
Dim hwnd As Integer
Dim wFunc As Integer
Dim pFrom As String
Dim pTo As String
Dim fFlags As Short
Dim fAnyOperationAborted As Boolean
Dim hNameMappings As Integer
Dim lpszProgressTitle As String
End Structure
Private Const SFC_DELETE As Short = &H3S
Private Const SFC_ALLOWUNDO As Short = &H40S
Private Const SFC_NOCONFIRMATION As Short = &H10S
Private Declare Function ShFileOperation _
Lib “shell32.dll” _
Alias “SHFileOperationA” _
(ByRef lpFileOp As ShFileOpStruct) As Integer
Public Function Ricicla(ByRef sPath As String) As Integer
Dim FileOp As ShFileOpStruct
If Not File.Exists(sPath) Then
MessageBox.Show(“File “ & sPath & ” non trovato.”)
Return -1
Exit Function
End If
With FileOp
.wFunc = SFC_DELETE
.pFrom = sPath & vbNullChar
.pTo = vbNullChar
.fFlags = SFC_NOCONFIRMATION Or SFC_ALLOWUNDO
.lpszProgressTitle = “Invio file “ & _
sPath & ” al Cestino di Windows.”
End With
Try
ShFileOperation(FileOp)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Return 0
End Function
End Module
Infine, per utilizzare effettivamente la funzione Ricicla potete fare riferimento al seguente esempio:
Dim esito As Integer = 0
esito = Ricicla(“C:\prova.txt”)
If esito = -1 Then
MessageBox.Show(“Errore: operazione annullata!”)
Else
MessageBox.Show(“Spostamento nel Cestino “ & _
“eseguito correttamente.”)
End If
L’utilizzo non richiede ulteriori commenti, dato che è sufficiente passare alla funzione Ricicla il nome del file completo del percorso.