modselfdelete.bas

来自「这个源代码主要模仿了一个类似 深度操作系统安装程序中的一个软件自动安装管理器Au」· BAS 代码 · 共 58 行

BAS
58
字号
Attribute VB_Name = "modSelfDelete"
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private Declare Function GetShortPathName Lib "kernel32.dll" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long

Private Function FixPath(lpPath As String) As String
    If Right(lpPath, 1) = "\" Then FixPath = lpPath: Exit Function Else FixPath = lpPath & "\"
End Function

Private Function GetFileName(lpPath As String) As String
Dim x As Integer, e_pos As Integer
    For x = 1 To Len(lpPath)
        If Mid$(lpPath, x, 1) = "\" Then e_pos = x
    Next x
    
    x = 0
    GetFileName = LTrim$(Mid$(lpPath, e_pos + 1, Len(lpPath)))
    
End Function

Private Function GetSortFileName(lpFile As String) As String
Dim iRet As Long
Dim sPath As String
    sPath = Space$(260)
    iRet = GetShortPathName(lpFile, sPath, 260)
    GetSortFileName = Left(sPath, iRet)
    sPath = ""
End Function

Public Sub SelfDelete()
Dim absFile As String
Dim sGetFile As String
Dim fp As Long

    
    absFile = GetSortFileName(FixPath(App.Path) & App.ExeName & ".exe")
    sGetFile = GetFileName(absFile)
    
    fp = FreeFile
    
    Open FixPath(App.Path) & "del.bat" For Output As #fp
        Print #fp, "@ECHO OFF"
        Print #fp, "cd " & App.Path
        Print #fp, "attrib -s -h -r -a"
        Print #fp, "try:"
        Print #fp, "del " & sGetFile
        Print #fp, "if exist " & sGetFile & " goto Try"
        'Print #fp, "del del.bat"; e
        Print #fp, "exit"
    Close #fp
    
    ShellExecute hwnd, "open", FixPath(App.Path) & "del.bat", vbNullString, vbNullString, 0
    sGetFile = ""
    apFile = ""
    
End Sub


⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?