If your program deletes any files, you should always try to use the
SHFileOperation API in order to move the file to the recycle bin. The use has an option to restore the file if neccessary !
GeSHi (vbnet):
Private Const FO_DELETE As Integer = 3
Private Const FOF_ALLOWUNDO As Integer = &H40
Private Const FOF_NOCONFIRMATION As Integer = &H10
Private Structure SHFILEOPSTRUCT
Dim hWnd As IntPtr
Dim wFunc As Integer
Dim pFrom As String
Dim pTo As String
Dim fFlags As Short
Dim fAborted As Boolean
Dim hNameMaps As IntPtr
Dim sProgress As String
End Structure
Private Declare Auto Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (ByRef lpFileOp As SHFILEOPSTRUCT) As Integer
Private Function DeleteToBin(ByVal sFile As String) As Boolean
Dim SHFileOp As SHFILEOPSTRUCT = New SHFILEOPSTRUCT()
SHFileOp.wFunc = FO_DELETE
SHFileOp.pFrom = sFile & Chr(0) & Chr(0)
SHFileOp.fFlags = FOF_ALLOWUNDO Or FOF_NOCONFIRMATION
Try
If SHFileOperation(SHFileOp) = 0 Then
Return True
Exit Function
End If
Catch
Return False
End Try
End Function
Created by GeSHI 1.0.7.20
Usage :
GeSHi (vbnet):
If DeleteToBin("c:\windows\testfile.exe") = True Then
MsgBox("File deleted to recycle bin!", MsgBoxStyle.Information, "Done...")
End If
Created by GeSHI 1.0.7.20