NSIS安装脚本简单学习

这篇博客详细介绍了如何使用NSIS(Nullsoft Scriptable Install System)脚本来创建安装和卸载程序。内容包括设置安装图标、定义语言、安装过程、程序注册、文件打包以及卸载逻辑等步骤,确保程序能够正确安装和卸载,并提供了相应的回调函数处理安装前后的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

; 注意 : 文件要保存为 UTF-8-BOM 编码 , 否则在执行此nsi脚本的时候会出现 Bad Text Encoding的错误 
# 包含头文件 
!include "MUI.nsh"
!include "LogicLib.nsh"

!define MUI_ABORTWARNING
!define MUI_ICON "./uires/logo.ico" #安装icon
!define MUI_UNICON "./uires/logo.ico" #卸载icon


!insertmacro MUI_PAGE_WELCOME #安装欢迎页面

!insertmacro MUI_PAGE_DIRECTORY #选择安装目录页面

!insertmacro MUI_LANGUAGE "SimpChinese" #安装界面显示的语言

!insertmacro MUI_PAGE_INSTFILES #安装过程页面

LangString ProductName ${LANG_SimpChinese} "PC客户端"
LangString UnInstallTip ${LANG_SimpChinese} "准备好卸载了吗" #卸载提示
LangString InstallTip ${LANG_SimpChinese} "准备好安装了吗" # 安装提示
LangString RunExeTip ${LANG_SimpChinese} "是否立即运行程序" # 运行程序提示
LangString FinishRunButtonText ${LANG_SimpChinese} "完成" 
LangString FinishCheckBoxText ${LANG_SimpChinese} "myapp.exe" 
LangString UninstallEnsure ${LANG_SimpChinese} "你确实要完全移除 $(ProductName) ,及其所有的组件?"
LangString UninstallResult ${LANG_SimpChinese} "$(ProductName) 已成功地从你的计算机移除。"

!define MUI_FINISHPAGE_TEXT $(RunExeTip)
!define MUI_FINISHPAGE_BUTTON $(FinishRunButtonText) #完成按钮的文案
!define MUI_FINISHPAGE_RUN_TEXT  $(FinishCheckBoxText) #运行应用程序旁边的文字
!define MUI_FINISHPAGE_RUN "$INSTDIR\myapp.exe" #安装完成运行程序
!insertmacro MUI_PAGE_FINISH #安装完成页面 define定义的属性 要在对应的页面 insertmacro 之前


; 安装程序初始定义常量
!define PRODUCT_NAME $(ProductName)

!define PRODUCT_VERSION "1.0.0.1"

!define PRODUCT_PUBLISHER $(ProductName)
!define PRODUCT_WEB_SITE "http://www.baidu.com"
!define PRODUCT_UNINST_ROOT_KEY "HKLM"
!define REG_NAME "myapp1"


Name "myapp" #安装界面显示的名称
OutFile "${REG_NAME}Install.exe" #输出的安装包的名称路径
InstallDir "D:\${REG_NAME}Install" #安装界面的选择目录时的默认安装目录

; 安装section 打包的时候(makenisi)程序会执行的地方
Section Install
    SetOutPath $INSTDIR
    File /r .\setup\*.dll #将此目录下的所有dll 打包进安装包
    File .\setup\myapp.exe
    ;协议注册启动程序
    WriteRegStr HKCR "${REG_NAME}" "" ""
    WriteRegStr HKCR "${REG_NAME}" "URL Protocol" ""
    WriteRegStr HKCR "${REG_NAME}\DefaultIcon" "" "${REG_NAME}.exe,1"
    WriteRegStr HKCR "${REG_NAME}\shell" "" ""
    WriteRegStr HKCR "${REG_NAME}\shell\open" "" ""
    WriteRegStr HKCR "${REG_NAME}\shell\open\command" "" '"$INSTDIR\myapp.exe" "%1"'
SectionEnd

; 卸载section 运行卸载程序会执行的地方
Section Uninstall
    ;MessageBox MB_ICONINFORMATION|MB_OK $(UnInstallTip) 
    DeleteRegKey HKCR "${REG_NAME}\shell\open\command"
    DeleteRegKey HKCR "${REG_NAME}\shell\open"
    DeleteRegKey HKCR "${REG_NAME}\shell"
    DeleteRegKey HKCR "${REG_NAME}\DefaultIcon"
    DeleteRegKey HKCR "${REG_NAME}"
    Delete "$INSTDIR\*.dll"
    Delete "$INSTDIR\myapp.exe"
    Delete "$INSTDIR\uninst.exe"
    ;RMDir /r "$INSTDIR"
SectionEnd

;卸载程序的输出
Section -Post
    WriteUninstaller ".\uninst.exe" #卸载程序 会在安装完成后写入到安装目录
SectionEnd    

/* 普通函数 可以用Call 来调用 */
Function NormalFunc
    MessageBox MB_ICONINFORMATION|MB_OK "normal func"
FunctionEnd

# 安装之前执行的回调函数
Function .onInit
    MessageBox MB_ICONINFORMATION|MB_OK $(InstallTip)
    Call NormalFunc
FunctionEnd

# 卸载之前执行的回调函数
Function un.onInit
    MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2 $(UninstallEnsure)  IDYES true IDNO false
    true:
        ;检测进程是否运行
        Goto done
    false:
        Abort
    done:
        Goto +1 ;Goto +1 goes to the next instruction, Goto -1 goes to the previous instruction, etc.
FunctionEnd

Function un.onUninstSuccess
    HideWindow
    MessageBox MB_ICONINFORMATION|MB_OK $(UninstallResult)
FunctionEnd

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程经验随笔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值