How to delete a file with VB using API?
PLZ. help us...
//DoomBringer
Printable View
How to delete a file with VB using API?
PLZ. help us...
//DoomBringer
Why not just use VB's Kill command? But if you have to use an API call, there's...
Private Declare Function DeleteFile Lib "kernel32" Alias "DeleteFileA" (ByVal lpFileName As String) As Long
Hopefully there's nothing funky about it, but I've never used it -- just looked it up so I can't promise anything.
VBs Kill command is the easiest, but another alternate would be to use the DeleteFile method of the FileSystemObject.
Well i would not reccomend using the FileSystemObject just for deleting a file.
kill command work like that :
kill ("c:\myproject.exe")
is that how we use it ? :confused:
Kill will delete the file, but not send it to the recycle bin. If you want to send it to the Recycle Bin, then you could use the SHFileOperation API.
VB Code:
Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long Private Type SHFILEOPSTRUCT hwnd As Long wFunc As Long pFrom As String pTo As String fFlags As Integer fAnyOperationsAborted As Long hNameMappings As Long lpszProgressTitle As String End Type Private Const FO_DELETE = &H3 Private Const FOF_ALLOWUNDO = &H40 Private Sub Command1_Click() Dim SH As SHFILEOPSTRUCT SH.hwnd = hwnd SH.wFunc = FO_DELETE SH.fFlags = FOF_ALLOWUNDO SH.pFrom = "C:\Windows\Desktop\MyFile.txt" SHFileOperation SH End Sub