首页 > 代码库 > 安装文件制作工具NSIS的使用总结

安装文件制作工具NSIS的使用总结

安装文件制作工具NSIS 使用总结
 
在给客户开发客户端软件时,为避免技术人员亲自上门安装带来额外的成本损耗,通常我们都会自作一个安装包,以确保我们开发的程序的相关依赖资源、环境在客户端运行前能自动地正确配置好。
 
NSIS是一个比较流行的安装文件制作工具,制作简单,提供脚本语言来定义环境和程序的静态资源配置,使得安装文件可定制化, 并能根据自定义的脚本文件自动生成可执行的安装包, 大大地简化了程序员的发布工作。
 
NSIS提供了多个脚本demo,可在UI上直接打开来查看其demo 并学习。
 
其脚本指令基本都用于设置安装程序的几大部件: 安装程序标题, 默认安装目录,待copy的文件, 目标地址及目录结构,运行环境设置:注册表, 卸载时的操作:删除文件,注册表。
 
因此其对应的脚本指令大概有以下几类:
1. 安装程序标题: Name
2. 默认安装目录:InstallDir
3. 待copy的文件: File
4. 目标地址及目录结构:InstallDir 
5. 运行环境设置:注册表,快捷方式: WriteRegStr,CreateShortCut
6. 卸载时的操作:删除文件,目录,注册表: Delete,RMDir, DeleteRegKey
 
 
注意点:
1.  如果程序需安装在C盘,在win vista后续版本需要获取 admin 权限。
2. 如需在目标地址下添加目录,则需在创建目录后,设置新的输出目录。
         CreateDirectory $INSTDIR\mplayer
         SetOutPath $INSTDIR\mplayer
 
 
 
脚本代码实例如下:
 
 ; VRA.nsi
;
; This script is based on VRA.nsi, but it remember the directory, 
; has uninstall support and (optionally) installs start menu shortcuts.
;
; It will install VRA.nsi into a directory that the user selects,

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

; The name of the installer
Name "VRA installer"
InstallDir "C:\VRA"

; The file to write
; OutFile "example2.exe"
OutFile "VRA_Installer.exe"
RequestExecutionLevel admin 


; The default installation directory
;InstallDir $PROGRAMFILES\VRA

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

; Request application privileges for Windows Vista
RequestExecutionLevel admin

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

; Pages

Page components
Page directory
Page instfiles

UninstPage uninstConfirm
UninstPage instfiles

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

; The stuff to install
Section "VRA (required)"

SectionIn RO

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

; Put file there
;File "example2.nsi"
File "bz2.pyd"
File "MSVCP90.dll"
File "MSVCR90.dll"
File "msvcrt.dll"
File "pycpuid._pycpuid.pyd"
File "python27.dll"
File "pywintypes27.dll"
File "screen_left.bmp"
File "select.pyd"
File "unicodedata.pyd"
File "user32.dll"
File "VRA.exe"
File "VRA.exe.manifest"
File "win32api.pyd"
File "win32evtlog.pyd"
File "wx._controls_.pyd"
File "wx._core_.pyd"
File "wx._gdi_.pyd"
File "wx._misc_.pyd"
File "wx._windows_.pyd"
File "wxbase30u_net_vc90.dll"
File "wxbase30u_vc90.dll"
File "wxmsw30u_adv_vc90.dll"
File "wxmsw30u_core_vc90.dll"
File "wxmsw30u_html_vc90.dll"
File "_ctypes.pyd"
File "_hashlib.pyd"
File "_socket.pyd"
File "_ssl.pyd"
File "mplayer.exe"
CreateDirectory $INSTDIR\mplayer
SetOutPath $INSTDIR\mplayer
File "mplayer\config"
SetOutPath $INSTDIR

; Write the installation path into the registry
WriteRegStr HKLM SOFTWARE\VRA "Install_Dir" "$INSTDIR"
WriteRegStr HKCU "Software\Microsoft\Windows\CurrentVersion\Run" Shell "$INSTDIR\VRA.exe"

; Write the uninstall keys for Windows
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\VRA" "VRA" "VRA"
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\VRA" "Uninstall_VRA" ‘"$INSTDIR\uninstall.exe"‘
WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\VRA" "NoModify" 1
WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\VRA" "NoRepair" 1
WriteUninstaller "uninstall.exe"

SectionEnd

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

CreateDirectory "$SMPROGRAMS\VRA"
CreateShortCut "$SMPROGRAMS\VRA\Uninstall.lnk" "$INSTDIR\uninstall.exe" "" "$INSTDIR\uninstall.exe" 0
CreateShortCut "$SMPROGRAMS\VRA\VRA.lnk" "$INSTDIR\VRA.exe" "" "$INSTDIR\VRA.exe" 0

SectionEnd



; Optional section (can be disabled by the user)
Section "Desktop Shortcuts" SectionX

SetShellVarContext current
CreateShortCut "$DESKTOP\VRA.lnk" "$INSTDIR\VRA.exe"
;WriteRegStr HKLM "Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\layers" "$INSTDIR\VRA.exe" "RUNASADMIN"
SectionEnd


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

; Uninstaller

Section "Uninstall"

; Remove registry keys
DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\VRA"
DeleteRegKey HKLM SOFTWARE\VRA

; Remove files and uninstaller
;Delete $INSTDIR\example2.nsi
;Delete $INSTDIR\uninstall.exe
Delete "$INSTDIR\bz2.pyd"
Delete "$INSTDIR\Microsoft.VC90.CRT.manifest"
Delete "$INSTDIR\msvcm90.dll"
Delete "$INSTDIR\msvcp90.dll"
Delete "$INSTDIR\msvcr90.dll"
Delete "$INSTDIR\msvcrt.dll"
Delete "$INSTDIR\pycpuid._pycpuid.pyd"
Delete "$INSTDIR\python27.dll"
Delete "$INSTDIR\pywintypes27.dll"
Delete "$INSTDIR\screen_left.bmp"
Delete "$INSTDIR\select.pyd"
Delete "$INSTDIR\unicodedata.pyd"
Delete "$INSTDIR\user32.dll"
Delete "$INSTDIR\VRA.exe"
Delete "$INSTDIR\VRA.exe.manifest"
Delete "$INSTDIR\win32api.pyd"
Delete "$INSTDIR\win32evtlog.pyd"
Delete "$INSTDIR\wx._controls_.pyd"
Delete "$INSTDIR\wx._core_.pyd"
Delete "$INSTDIR\wx._gdi_.pyd"
Delete "$INSTDIR\wx._misc_.pyd"
Delete "$INSTDIR\wx._windows_.pyd"
Delete "$INSTDIR\wxbase30u_net_vc90.dll"
Delete "$INSTDIR\wxbase30u_vc90.dll"
Delete "$INSTDIR\wxmsw30u_adv_vc90.dll"
Delete "$INSTDIR\wxmsw30u_core_vc90.dll"
Delete "$INSTDIR\wxmsw30u_html_vc90.dll"
Delete "$INSTDIR\_ctypes.pyd"
Delete "$INSTDIR\_hashlib.pyd"
Delete "$INSTDIR\_socket.pyd"
Delete "$INSTDIR\_ssl.pyd"
Delete "$INSTDIR\mplayer.exe"
Delete "$INSTDIR\mplayer\config"
Delete "init.check"
Delete "init.video"
Delete "kamhearing.log"
Delete "player_reg.dll"
Delete "uninstall.exe"

; Remove shortcuts, if any
Delete "$SMPROGRAMS\VRA\*.*"
Delete "$SMPROGRAMS\VRA\VRA.lnk"

; Remove directories used
RMDir "$SMPROGRAMS\VRA"
RMDir "$INSTDIR\mplayer"
RMDir "$INSTDIR"


SectionEnd

安装文件制作工具NSIS的使用总结