1、在普通安装脚本前添加库
!include "LogicLib.nsh"
2、; 激活安装日志记录,该日志文件将会作为卸载文件的依据(注意,本区段必须放置在所有区段之前)
Section "-LogSetOn" LogSet on SectionEnd
3、
/****************************** * 以下是安装程序的卸载部分 * ******************************/ ; 根据安装日志卸载文件的调用宏 !macro DelFileByLog LogFile ifFileExists `${LogFile}` 0 +4 Push `${LogFile}` Call un.DelFileByLog Delete `${LogFile}` !macroend Section Uninstall !insertmacro MUI_STARTMENU_GETFOLDER "Application" $ICONS_GROUP Delete "$INSTDIR${PRODUCT_NAME}.url" ; 调用宏只根据安装日志卸载安装程序自己安装过的文件 !insertmacro DelFileByLog "$INSTDIRinstall.log" ; 清除安装程序创建的且在卸载时可能为空的子目录,对于递归添加的文件目录,请由最内层的子目录开始清除(注意,不要带 /r 参数,否则会失去 DelFileByLog 的意义) RMDir "$SMPROGRAMS$ICONS_GROUP" SectionEnd #-- 根据 NSIS 脚本编辑规则,所有 Function 区段必须放置在 Section 区段之后编写,以避免安装程序出现未可预知的问题。--# ; 以下是卸载程序通过安装日志卸载文件的专用函数,请不要随意修改 Function un.DelFileByLog Exch $R0 Push $R1 Push $R2 Push $R3 FileOpen $R0 $R0 r ${Do} FileRead $R0 $R1 ${IfThen} $R1 == `` ${|} ${ExitDo} ${|} StrCpy $R1 $R1 -2 StrCpy $R2 $R1 11 StrCpy $R3 $R1 20 ${If} $R2 == "File: wrote" ${OrIf} $R2 == "File: skipp" ${OrIf} $R3 == "CreateShortCut: out:" ${OrIf} $R3 == "created uninstaller:" Push $R1 Push `"` Call un.DelFileByLog.StrLoc Pop $R2 ${If} $R2 != "" IntOp $R2 $R2 + 1 StrCpy $R3 $R1 "" $R2 Push $R3 Push `"` Call un.DelFileByLog.StrLoc Pop $R2 ${If} $R2 != "" StrCpy $R3 $R3 $R2 Delete /REBOOTOK $R3 ${EndIf} ${EndIf} ${EndIf} StrCpy $R2 $R1 7 ${If} $R2 == "Rename:" Push $R1 Push "->" Call un.DelFileByLog.StrLoc Pop $R2 ${If} $R2 != "" IntOp $R2 $R2 + 2 StrCpy $R3 $R1 "" $R2 Delete /REBOOTOK $R3 ${EndIf} ${EndIf} ${Loop} FileClose $R0 Pop $R3 Pop $R2 Pop $R1 Pop $R0 FunctionEnd Function un.DelFileByLog.StrLoc Exch $R0 Exch Exch $R1 Push $R2 Push $R3 Push $R4 Push $R5 StrLen $R2 $R0 StrLen $R3 $R1 StrCpy $R4 0 ${Do} StrCpy $R5 $R1 $R2 $R4 ${If} $R5 == $R0 ${OrIf} $R4 = $R3 ${ExitDo} ${EndIf} IntOp $R4 $R4 + 1 ${Loop} ${If} $R4 = $R3 StrCpy $R0 "" ${Else} StrCpy $R0 $R4 ${EndIf} Pop $R5 Pop $R4 Pop $R3 Pop $R2 Pop $R1 Exch $R0 FunctionEnd