Is There a Difference Between the DeleteFile API and the Kill Function?
Printable View
Is There a Difference Between the DeleteFile API and the Kill Function?
I may be wrong here (guessing) but I think with the API you can send the file to recycle bin.
There is practically no difference except that DeleteFile() requires special declaration. ;) Also, DeleteFile() will NOT generate any errors if file doesn't exist and Kill statement will ... so maybe that's the only advantage.
Does anyone know how to delete hidden files without having to change the attributes?
not 100% sure if you can do that. setting the attributes in VB is easy tho.
Private Sub Command1_Click()
On Error GoTo KillErr:
SetAttr "C:\filename.txt", vbNormal
Kill "C:\filename.txt"
Exit Sub
KillErr:
If Err Then MsgBox Err.Description
End Sub
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