Windows 程序打包工具NSIS使用

介绍

NSIS (Nullsoft Scriptable Install System) 是一个免费开源的 Windows
安装程序制作工具,它允许开发者创建轻量级、可定制的安装程序。NSIS 使用类似于脚本语言的方式编写安装程序逻辑,非常适合创建复杂的安装流程。

安装

  • 访问官方网站:
    打开 NSIS 的官方网站:https://nsis.sourceforge.io/
  • 选择版本:
    在下载页面上,选择适合你的 Windows 操作系统的 NSIS 版本。
  • 下载安装包:
    选择合适的 NSIS 安装包进行下载。通常,你会看到一个 .exe 文件,这是 NSIS 的安装程序。
  • 下载完成后,双击运行下载的安装程序。
  • 安装 NSIS:
    按照安装向导的提示完成安装过程。
    在这里插入图片描述

使用步骤

  • 创建 NSIS 脚本文件
    使用文本编辑器(如 Notepad++ 或 Visual Studio Code)创建一个新的 .nsh 文件。
  • 编写 NSIS 脚本
    NSIS 脚本由一系列指令组成,这些指令定义了安装程序的行为。
		常见的指令包括于:
		OutFile: 设置输出文件名。
		InstallDir: 设置安装目录。
		RequestExecutionLevel: 设置安装程序所需的权限级别。
		Page: 定义安装程序的页面,如许可协议、目录选择等。
		Section: 定义安装的不同部分。
		File: 指定要安装的文件。
		CreateDirectory: 创建目录。
		Write: 写入注册表项或文件。
  • 编译 NSIS 脚本
保存你的 NSIS 脚本文件。
打开命令提示符或终端。
导航到 NSIS 的安装目录。
使用 makensis.exe 命令编译你的 NSIS 脚本文件
makensis yourscript.nsh

在这里插入图片描述

例子

;
; This script is based on example1.nsi, but it remember the directory, 
; has uninstall support and (optionally) installs start menu shortcuts.
;
!define /date CUR_TIME "%Y%m%d_%H%M%S"
; It will install example2.nsi into a directory that the user selects,
; 标题名称
Caption "软件名称 安装向导"
; 标牌的内容
BrandingText "xxxx有限公司"
; 安装程序图标
Icon ".\xxx.ico"
;--------------------------------
; The name of the installer
Name "软件名称"

; The file to write
OutFile "xxxx_kaibo_${CUR_TIME}.exe"

; The default installation directory
InstallDir $PROGRAMFILES\xxxx\软件名称

; Registry key to check for directory (so if you install again, it will 
; overwrite the old one automatically)
InstallDirRegKey HKLM "Software\软件名称" "Install_Dir"

; Request application privileges for Windows Vista
RequestExecutionLevel admin

;--------------------------------

; Pages
;Page license
Page components
Page directory
Page instfiles

UninstPage uninstConfirm
UninstPage instfiles

;--------------------------------

; The stuff to install
Section "软件名称 (required)"

  SectionIn RO
  
  ; Set output path to the installation directory.
  SetOutPath $INSTDIR
  
  ; Put file there
  SetOverwrite on
  File "..\Release\*.exe"
  File "..\Release\*.dll"
  SetOverwrite off
  File "..\Release\xxxxxx.ini"
  SetOverwrite on
  File "..\Release\xxxxx.pdb"
  ;File "..\Release\*.pdb"
  ;File "..\Release\*.pak"
  ;File "..\Release\*.conf"

  
  ;SetOutPath $INSTDIR\skin
  File /r "..\Release\skin"
  File /r "..\release\temp_img"
  File /r "..\Release\httpdoc"
  File /r "..\Release\web"
  ;File /r "..\Release\dist"
  
  SetOutPath $INSTDIR
  
  ; Write the installation path into the registry
  WriteRegStr HKLM SOFTWARE\软件名称 "Install_Dir" "$INSTDIR"
  
  ; Write the uninstall keys for Windows
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\软件名称" "DisplayName" "软件名称"
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\软件名称" "UninstallString" '"$INSTDIR\uninstall.exe"'
  WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\软件名称" "NoModify" 1
  WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\软件名称" "NoRepair" 1
  WriteUninstaller "uninstall.exe"
  
SectionEnd

; Optional section (can be disabled by the user)
Section "Start Menu Shortcuts"

  CreateDirectory "$SMPROGRAMS\xxxx\软件名称"
  CreateShortCut "$SMPROGRAMS\xxxx\软件名称\Uninstall.lnk" "$INSTDIR\uninstall.exe" "" "$INSTDIR\uninstall.exe" 0
  CreateShortCut "$SMPROGRAMS\xxxx\软件名称\软件名称.lnk" "$INSTDIR\CRLiveStudio.exe" "" "$INSTDIR\CRLiveStudio.exe" 0
  CreateShortCut "$DESKTOP\软件名称.lnk" "$INSTDIR\CRLiveStudio.exe" "" "$INSTDIR\CRLiveStudio.exe"

SectionEnd

;--------------------------------

; Uninstaller

Section "Uninstall"
  
  ; Remove registry keys
  DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\软件名称"
  DeleteRegKey HKLM SOFTWARE\软件名称

  ; Remove files and uninstaller
  Delete $INSTDIR\*.exe
  Delete $INSTDIR\*.ini
  Delete $INSTDIR\*.log
  Delete $INSTDIR\*.txt
  Delete $INSTDIR\*.sqlite3
  Delete $INSTDIR\*.dll
  Delete $INSTDIR\*.pdb
  Delete $INSTDIR\*.pak
  Delete $INSTDIR\*.bin
  Delete $INSTDIR\*.dat
  Delete $INSTDIR\*.conf
  Delete $INSTDIR\skin\*.*
  Delete $INSTDIR\temp_img\*.*
  Delete $INSTDIR\httpdoc\*.*
  Delete $INSTDIR\web\*.*
  Delete $INSTDIR\Log\*.*
  ;Delete $INSTDIR\locales\*.*
  ;Delete $INSTDIR\swiftshader\*.*
  ;Delete $INSTDIR\Browsercache\*.*
	
  ; Remove shortcuts, if any
  Delete "$SMPROGRAMS\xxxx\软件名称\*.*"
  Delete "$DESKTOP\软件名称.lnk"

  ; Remove directories used
  RMDir "$SMPROGRAMS\xxxx\软件名称"
  RMDir /r "$INSTDIR\skin"
  RMDir /r "$INSTDIR\temp_img"
  RMDir /r "$INSTDIR\httpdoc"
  RMDir /r "$INSTDIR\web"
  ;RMDir /r "$INSTDIR\swiftshader"
  RMDir /r "$INSTDIR\Log"
  ;RMDir /r "$INSTDIR\dist"
  ;RMDir /r "$INSTDIR\Browsercache"
  RMDir "$INSTDIR"
  
SectionEnd

Function .onInstSuccess
    Exec $INSTDIR\CRLiveStudio.exe
FunctionEnd

; 安装程序初始化设置 Function .onInit
Function .onInit
; 发现有程序正在运行时提示退出后继续运行

loop:
FindWindow $R0 "#32770" "软件名称"
IntCmp $R0 0 done
MessageBox MB_RETRYCANCEL \
"软件名称 正在运行。请关闭 软件名称 后点击“重试”按钮继续安装,点击“取消”按钮将退出安装程序" \
IDRETRY NoAbort
Abort ; causes installer to quit.
NoAbort:
; 稍等后继续检测直至检测不到或用户选择取消
Sleep 444
Goto loop
done:
FunctionEnd

Function un.onInit
; 发现有程序正在运行时提示退出后继续运行

loop:
FindWindow $R0 "#32770" "软件名称"
IntCmp $R0 0 done
MessageBox MB_RETRYCANCEL \
"软件名称 正在运行。请关闭 软件名称 后点击“重试”按钮继续卸载,点击“取消”按钮将退出卸载程序" \
IDRETRY NoAbort
Abort ; causes installer to quit.
NoAbort:
; 稍等后继续检测直至检测不到或用户选择取消
Sleep 444
Goto loop
done:
FunctionEnd
  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值