Sending Files to Recycle Bin

  • Thread starter Thread starter Dinesh Jain
  • Start date Start date
D

Dinesh Jain

Hi all,
File.Delete(fileName) causes file to be deleted permanently. How to
send it to Recycle bin?

Please help,
Thanks in advance,

-Regards,
Dinesh
 
Hi,

You have use the SHFileOperation API to do that.

The api declare



<DllImport("shell32.dll", entrypoint:="SHFileOperationA", _

setlasterror:=True, CharSet:=CharSet.Auto, exactspelling:=True, _

CallingConvention:=CallingConvention.StdCall)> _

Public Shared Function SHFileOperation(ByRef lpFileOp As SHFILEOPSTRUCT) As
Integer

End Function



The structure



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 Integer

Dim hNameMappings As Integer

Dim lpszProgressTitle As String ' only used if FOF_SIMPLEPROGRESS

End Structure



The constants



Private Const FO_DELETE = &H3

Private Const FOF_ALLOWUNDO = &H40



And finally the same code.



Dim sh As New SHFILEOPSTRUCT

With sh

.wFunc = FO_DELETE

.pFrom = FileName

.fFlags = FOF_ALLOWUNDO

End With

If SHFileOperation(sh) <> 0 Then

MessageBox.Show("Error deleting file")

End If

Ken
 
Back
Top