DeleteFile() returns zero if an error occurs, such as if the file does not exist. If it matters to you, you must trap the return value.
VB Code:
Dim lRet As Long
lRet = DeleteFile([filename])
If (lRet) Then
' succeeded
Else
' didn't
End If
To delete a file with the option to send it to the recycle bin you must use the SHFileOperation() API.
VB Code:
Declare Function SHFileOperation Lib "shell32" Alias "SHFileOperationA" ( _
ByRef lpFileOp As SHFILEOPSTRUCT _
) As Long
Private Type SHFILEOPSTRUCT
hWnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAborted As Boolean
hNameMaps As Long
sProgress As String
End Type
Const FOF_ALLOWUNDO = &H40
Const FOF_NOCONFIRMATION = &H10
Const FO_DELETE = &H3
' Usage: -------------------------------------
Dim udtFileOp As SHFILEOPSTRUCT
With udtFileOp
.wFunc = FO_DELETE
' pFrom is a null-delimited string containing the files to delete.
' In addition, the final filename must have an extra null char after it.
.pFrom = [filename] & vbNullChar & vbNullChar
' add Or FOF_NOCONFIRMATION to suppress the Are You Sure box.
.fFlags = FOF_ALLOWUNDO
End With
' Perform the delete
Dim lRet As Long
lRet = SHFileOperation(udtFileOp)
If (lRet) Then
' An error occured
End If