程序运行过程中不能删除自身,只能在退出程序时删除自身。
程序临结束前,加入以下语句
Dim FileNumber As Integer, spath As String, sapp As String
FileNumber = FreeFile
spath = App.Path & "\"
sapp = spath & App.EXEName & ".exe"
Open spath & "\" & "del.bat" For Output As FileNumber
Print #FileNumber, ":try" & Chr(13)
Print #FileNumber, "del " & sapp & Chr(13)
Print #FileNumber, "if exist " & sapp & " goto try'" & Chr(13)
Print #FileNumber, "del %0" & Chr(13)
Close #FileNumber
FileNumber = Shell(spath & "\" & "del.bat", 0)
End

解决方案 »

  1.   

    只需要在最后的代码处建立一个批处理文件,让该文件删除EXE程序,然后再删除自身。如'声明API函数Private Declare Function WinExec Lib "kernel32" (ByVal lpCmdLine As String, ByVal nCmdShow As Long) As LongPrivate Sub 过程名..........'代码最后处open "uninstall.bat" for output as #FileNumPrint #FileNum, "del EXE文件"  '删除EXE文件
    Print #FileNum, "del %0"     '删除建立的批处理文件
    Close #FileNum
    WinExec "uninstall.bat", 0    '调用API函数执行批处理文件
    EndEnd Sub