0%

NSIS 简单配置

NSIS(Nullsoft Scriptable Install System)是一个开源的 Windows 系统下安装程序制作程序。它提供了安装、卸载、系统设置、文件解压缩等功能。这如其名字所指出的那样,NSIS 是通过它的脚本语言来描述安装程序的行为和逻辑的。NSIS 的脚本语言和通常的编程语言有类似的结构和语法,但它是为安装程序这类应用所设计的。

制作安装包

下载地址 https://nsis.sourceforge.io/Download

因为功能比较多,建议看官方文档,这里简单列一个例子,方便照抄借鉴。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
; example1.nsi
;
; This script is perhaps one of the simplest NSIs you can make. All of the
; optional settings are left to their default settings. The installer simply
; prompts the user asking them where to install, and drops a copy of example1.nsi
; there.

;--------------------------------
;Include Modern UI
!include "MUI2.nsh"
;--------------------------------

; The name of the installer
Name "安装包标题(程序名或者自定义)"

; The file to write
OutFile "安装包名字(程序名或者自定义).exe"

; The default installation directory
InstallDir $PROGRAMFILES\程序名或者自定义键值

; Request application privileges for Windows Vista
RequestExecutionLevel admin

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

; Pages

;Page components
;Page directory
;Page instfiles

!define MUI_ICON "favicon.ico"
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!define MUI_FINISHPAGE_RUN "$INSTDIR\paperless.exe"
!define MUI_FINISHPAGE_RUN_TEXT "启动 安装包名字(程序名或者自定义)"
!define MUI_FINISHPAGE_RUN_NOTCHECKED
!insertmacro MUI_PAGE_FINISH

!insertmacro MUI_LANGUAGE SimpChinese

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

; The stuff to install
Section "" ;No components page, name is not important

; 设置开机启动
WriteRegStr HKCU "Software\Microsoft\Windows\CurrentVersion\Run" "程序名或者自定义键值" "$INSTDIR\程序名.exe"

; write uninstall strings
; 加入注册表
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\程序名或者自定义键值" "DisplayName" "程序名或者自定义键值"
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\程序名或者自定义键值" "UninstallString" '"$INSTDIR\uninstall.exe"'

; Set output path to the installation directory.
SetOutPath $INSTDIR

; Put file there
; 可以多个
File 文件名

; 注册动态库或者OCX
RegDLL "$INSTDIR\动态库或ocx名字"

SetOutPath $INSTDIR

; 创建卸载程序
WriteUninstaller "uninstall.exe"
SectionEnd ; end the section

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

; 创建开始菜单文件夹
CreateDirectory "$SMPROGRAMS\程序名或者自定义文件夹"
; 创建快捷方式到开始菜单
CreateShortcut "$SMPROGRAMS\程序名或者自定义文件夹\Uninstall.lnk" "$INSTDIR\uninstall.exe" "" "$INSTDIR\uninstall.exe" 0
; 创建快捷方式到开始菜单
CreateShortcut "$SMPROGRAMS\程序名或者自定义文件夹\程序名.lnk" "$INSTDIR\程序名.exe" "" "$INSTDIR\程序名.exe" 0

SectionEnd

Section "Uninstall"

; 删除开机启动注册表
DeleteRegValue HKCU "Software\Microsoft\Windows\CurrentVersion\Run" "程序名或者自定义键值"
; 删除注册表
DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\程序名或者自定义键值"

; 杀死进程才能删除
nsExec::Exec "taskkill /F /IM 程序名.exe"

; 取消注册
UnRegDLL "$INSTDIR\动态库或ocx名字"

; Remove shortcuts, if any
Delete "$INSTDIR\*.*"

; 删除目录里面的内容
Delete "$SMPROGRAMS\程序目录\*.*"
; 删除目录
RMDir "$SMPROGRAMS\程序目录"
RMDir "$INSTDIR"

IfFileExists "$INSTDIR" 0 NoErrorMsg
MessageBox MB_OK "卸载完成" IDOK 0 ; skipped if file doesn't exist
NoErrorMsg:

SectionEnd

Var UNINSTALL_PROG
Function .onInit
MessageBox MB_YESNO "提示 内容 " IDYES NoAbort
Abort ; causes installer to quit.
NoAbort:
nsExec::Exec "taskkill /F /IM 进程名.exe"
; 以下为 检查已安装并卸载
ClearErrors
ReadRegStr $UNINSTALL_PROG HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\程序名或者自定义键值" "UninstallString"
IfErrors done
ExecWait '"$UNINSTALL_PROG"' $0
FunctionEnd

参考文档

  • 官方文档
  • 基础文档 : NSIS 打包脚本基础
  • 截图详细 : 使用NSIS打包程序

其他互联网文档