NSIS 安装脚本(备忘)
; 该脚本使用 HM VNISEdit 脚本编辑器向导产生 ; 安装程序初始定义常量 !define PRODUCT_NAME "管理系统" !define PRODUCT_VERSION "1.0" !define PRODUCT_PUBLISHER "信息科技有限公司" !define PRODUCT_WEB_SITE "http://www.公司.com" !define PRODUCT_DIR_REGKEY "Software\Microsoft\Windows\CurrentVersion\App Paths\可执行文件.exe" !define PRODUCT_UNINST_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}" !define PRODUCT_UNINST_ROOT_KEY "HKLM" SetCompressor lzma ; ------ MUI 现代界面定义 (1.67 版本以上兼容) ------ !include "MUI.nsh" !include "nsDialogs.nsh" ; MUI 预定义常量 !define MUI_ABORTWARNING !define MUI_ICON "C:\本地路径\安装\install.ico" !define MUI_UNICON "C:\本地路径\安装\uninstall.ico" ; 欢迎页面 !insertmacro MUI_PAGE_WELCOME ; 安装目录选择页面 !insertmacro MUI_PAGE_DIRECTORY ; 安装过程页面 !insertmacro MUI_PAGE_INSTFILES ; 安装完成页面 !define MUI_FINISHPAGE_RUN "$INSTDIR\可执行文件.exe" !define MUI_FINISHPAGE_SHOWREADME "" !define MUI_FINISHPAGE_SHOWREADME_NOTCHECKED !define MUI_FINISHPAGE_SHOWREADME_TEXT "开机启动" !define MUI_FINISHPAGE_SHOWREADME_FUNCTION finishpageaction ;Page custom PageOtherTask !insertmacro MUI_PAGE_FINISH ; 安装卸载过程页面 !insertmacro MUI_UNPAGE_INSTFILES ; 安装界面包含的语言设置 !insertmacro MUI_LANGUAGE "SimpChinese" ; 安装预释放文件 !insertmacro MUI_RESERVEFILE_INSTALLOPTIONS ; ------ MUI 现代界面定义结束 ------ Name "${PRODUCT_NAME} ${PRODUCT_VERSION}" OutFile "setup.exe" InstallDir "$PROGRAMFILES\公司\产品" InstallDirRegKey HKLM "${PRODUCT_UNINST_KEY}" "UninstallString" ShowInstDetails show ShowUnInstDetails show BrandingText "信息科技有限公司" RequestExecutionLevel admin ;RequestExecutionLevel admin #NOTE: You still need to check user rights with UserInfo! Section "MainSection" SEC01 setShellVarContext all SetOutPath "$INSTDIR" SetOverwrite ifnewer CreateDirectory "$SMPROGRAMS\公司\产品" CreateShortCut "$SMPROGRAMS\公司\产品\${PRODUCT_NAME}.lnk" "$INSTDIR\可执行文件.exe" CreateShortCut "$DESKTOP\${PRODUCT_NAME}.lnk" "$INSTDIR\可执行文件.exe" File "本地文件" 。。。 。。。 。。。 。。。 CreateDirectory "$INSTDIR\Log" SetOutPath "$INSTDIR\Log" File "本地文件" SectionEnd Section -AdditionalIcons CreateShortCut "$SMPROGRAMS\公司\产品\Uninstall.lnk" "$INSTDIR\uninst.exe" SectionEnd Section -Post WriteUninstaller "$INSTDIR\uninst.exe" WriteRegStr HKLM "${PRODUCT_DIR_REGKEY}" "" "$INSTDIR\可执行文件.exe" WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayName" "$(^Name)" WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "UninstallString" "$INSTDIR\uninst.exe" WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayIcon" "$INSTDIR\可执行文件.exe" WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayVersion" "${PRODUCT_VERSION}" WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "URLInfoAbout" "${PRODUCT_WEB_SITE}" WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "Publisher" "${PRODUCT_PUBLISHER}" SectionEnd /****************************** * 以下是安装程序的卸载部分 * ******************************/ Section Uninstall setShellVarContext all Delete "$INSTDIR\文件.exe" Delete "$INSTDIR\文件.exe" Delete "$INSTDIR\文件.config" Delete "$INSTDIR\文件.dll" Delete "$SMPROGRAMS\公司\产品\Uninstall.lnk" Delete "$DESKTOP\${PRODUCT_NAME}.lnk" Delete "$SMPROGRAMS\公司\产品\${PRODUCT_NAME}.lnk" RMDir "$SMPROGRAMS\公司\产品" RMDir "$INSTDIR\Log" RMDir "$INSTDIR" DeleteRegKey ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" DeleteRegKey HKLM "${PRODUCT_DIR_REGKEY}" DeleteRegKey HKCU "SOFTWARE\Microsoft\Windows\CurrentVersion\Run\产品" SetAutoClose true SectionEnd #-- 根据 NSIS 脚本编辑规则,所有 Function 区段必须放置在 Section 区段之后编写,以避免安装程序出现未可预知的问题。--# Function .onInit FindProcDLL::FindProc "可执行文件.exe" Sleep 500 Pop $R0 ${If} $R0 != 0 KillProcDLL::KillProc "可执行文件.exe" ${EndIf} FindProcDLL::FindProc "Update.exe" Sleep 500 Pop $R0 ${If} $R0 != 0 KillProcDLL::KillProc "Update.exe" ${EndIf} FunctionEnd Function un.onInit MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2 "您确实要完全移除 $(^Name) ,及其所有的组件?" IDYES +2 Abort FunctionEnd Function un.onUninstSuccess HideWindow MessageBox MB_ICONINFORMATION|MB_OK "$(^Name) 已成功地从您的计算机移除。" FunctionEnd Function finishpageaction WriteRegStr HKCU "SOFTWARE\Microsoft\Windows\CurrentVersion\Run" "产品" "$INSTDIR\可执行文件.exe" FunctionEnd
以上是正常情况,没有判断.net环境
如果我们需要判断.net环境是事安装,需要以下脚本
Function GetNetFrameworkVersion ;获取.Net Framework版本,支持 Push $1 Push $0 ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5" "Install" ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5" "Version" StrCmp $1 "" +1 +2 StrCpy $1 "3.5" StrCmp $0 1 KnowNetFrameworkVersion +1 ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.0\Setup" "InstallSuccess" ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.0\Setup" "Version" StrCmp $1 "" +1 +2 StrCpy $1 "3.0" StrCmp $0 1 KnowNetFrameworkVersion +1 ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v2.0.50727" "Install" ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v2.0.50727" "Version" StrCmp $1 "" +1 +2 StrCpy $1 "2.0.50727.832" StrCmp $0 1 KnowNetFrameworkVersion +1 ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v1.1.4322" "Install" ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v1.1.4322" "Version" StrCmp $1 "" +1 +2 StrCpy $1 "1.1.4322.573" StrCmp $0 1 KnowNetFrameworkVersion +1 ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\.NETFramework\policy\v1.0" "Install" ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\.NETFramework\policy\v1.0" "Version" StrCmp $1 "" +1 +2 StrCpy $1 "1.0.3705.0" StrCmp $0 1 KnowNetFrameworkVersion +1 StrCpy $1 "not .NetFramework" KnowNetFrameworkVersion: Pop $0 Exch $1 FunctionEnd Function CheckFramework4 ;Clinet 和 Full存在一个都说明安装了Framework ClearErrors ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Client" "Install2" IfErrors 0 ExitCheckFramework4 ClearErrors ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full" "Install" IfErrors 0 ExitCheckFramework4 ;没有安装Framework MessageBox MB_OK "装程序无法检测到.NET Framework V4,这是必须安装的。安装程序将中止。" Abort ExitCheckFramework4: FunctionEnd
由于nsis要求所有function必须在section之后,因此,这些自定义函数一般放到最后
在section之前加一个.net判断的section
Section -.NET Call GetNetFrameworkVersion Pop $R1 ${If} $R1 < '3.5' NSISdl::download /TRANSLATE2 '正在下载 %s' '正在连接...' '(剩余 1 秒)' '(剩余 1 分钟)' '(剩余 1 小时)' '(剩余 %u 秒)' '(剩余 %u 分钟)' '(剩余 %u 小时)' '已完成:%skB(%d%%) 大小:%skB 速度:%u.%01ukB/s' /TIMEOUT=7500 /NOIEPROXY 'http://192.168.200.195/dotNetFx40_Full_x86_x64.exe' '$TEMP\dotNetFx40_Full_x86_x64.exe' Pop $R0 StrCmp $R0 "success" 0 +3 ;MessageBox MB_YESNO|MB_ICONQUESTION "dotNetFx40_Full_x86_x64.exe 已成功下载。$\r$\n是否立即安装?" IDNO +2 SetDetailsPrint textonly DetailPrint "正在安装 .NET Framework ..." SetDetailsPrint listonly ExecWait '$TEMP\dotNetFx40_Full_x86_x64.exe /norestart' $R1 Delete "$TEMP\dotNetFx40_Full_x86_x64.exe" ${If} $R1 != 0 MessageBox MB_OK "装程序无法检测到.NET Framework,这是必须安装的。安装程序将中止。" ;Abort Quit ${EndIf} ${EndIf} SectionEnd
以上代码会检察.net是否安装与.net判断,并自动从指定url下载并安装
当需要把.net打包进安装程序时(此时会大副增加安装文件体积)
Section -.NET Call GetNetFrameworkVersion Pop $R1 ${If} $R1 < '3.5' SetDetailsPrint textonly DetailPrint "正在安装 .NET Framework ..." SetDetailsPrint listonly SetOutPath "$TEMP" SetOverwrite on File "C:\。。。。\安装\dotNetFx40_Full_x86_x64.exe" ExecWait '$TEMP\dotNetFx40_Full_x86_x64.exe /norestart' $R1 Delete "$TEMP\dotNetFx40_Full_x86_x64.exe" ${If} $R1 != 0 MessageBox MB_OK "装程序无法检测到.NET Framework,这是必须安装的。安装程序将中止。" ;Abort Quit ${EndIf} ${EndIf} SectionEnd