Sending Files to Recycle Bin

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
 
K

Ken Tucker [MVP]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top