安装文件制作工具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 $INSTDIRmplayer
SetOutPath $INSTDIRmplayer
SetOutPath $INSTDIRmplayer
脚本代码实例如下:
; 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 $PROGRAMFILESVRA
; Registry key to check for directory (so if you install again, it will
; overwrite the old one automatically)
InstallDirRegKey HKLM "SoftwareVRA" "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 $INSTDIRmplayer
SetOutPath $INSTDIRmplayer
File "mplayerconfig"
SetOutPath $INSTDIR
; Write the installation path into the registry
WriteRegStr HKLM SOFTWAREVRA "Install_Dir" "$INSTDIR"
WriteRegStr HKCU "SoftwareMicrosoftWindowsCurrentVersionRun" Shell "$INSTDIRVRA.exe"
; Write the uninstall keys for Windows
WriteRegStr HKLM "SoftwareMicrosoftWindowsCurrentVersionUninstallVRA" "VRA" "VRA"
WriteRegStr HKLM "SoftwareMicrosoftWindowsCurrentVersionUninstallVRA" "Uninstall_VRA" '"$INSTDIRuninstall.exe"'
WriteRegDWORD HKLM "SoftwareMicrosoftWindowsCurrentVersionUninstallVRA" "NoModify" 1
WriteRegDWORD HKLM "SoftwareMicrosoftWindowsCurrentVersionUninstallVRA" "NoRepair" 1
WriteUninstaller "uninstall.exe"
SectionEnd
; Optional section (can be disabled by the user)
Section "Start Menu Shortcuts"
CreateDirectory "$SMPROGRAMSVRA"
CreateShortCut "$SMPROGRAMSVRAUninstall.lnk" "$INSTDIRuninstall.exe" "" "$INSTDIRuninstall.exe" 0
CreateShortCut "$SMPROGRAMSVRAVRA.lnk" "$INSTDIRVRA.exe" "" "$INSTDIRVRA.exe" 0
SectionEnd
; Optional section (can be disabled by the user)
Section "Desktop Shortcuts" SectionX
SetShellVarContext current
CreateShortCut "$DESKTOPVRA.lnk" "$INSTDIRVRA.exe"
;WriteRegStr HKLM "SoftwareMicrosoftWindows NTCurrentVersionAppCompatFlagslayers" "$INSTDIRVRA.exe" "RUNASADMIN"
SectionEnd
;--------------------------------
; Uninstaller
Section "Uninstall"
; Remove registry keys
DeleteRegKey HKLM "SoftwareMicrosoftWindowsCurrentVersionUninstallVRA"
DeleteRegKey HKLM SOFTWAREVRA
; Remove files and uninstaller
;Delete $INSTDIRexample2.nsi
;Delete $INSTDIRuninstall.exe
Delete "$INSTDIRz2.pyd"
Delete "$INSTDIRMicrosoft.VC90.CRT.manifest"
Delete "$INSTDIRmsvcm90.dll"
Delete "$INSTDIRmsvcp90.dll"
Delete "$INSTDIRmsvcr90.dll"
Delete "$INSTDIRmsvcrt.dll"
Delete "$INSTDIRpycpuid._pycpuid.pyd"
Delete "$INSTDIRpython27.dll"
Delete "$INSTDIRpywintypes27.dll"
Delete "$INSTDIRscreen_left.bmp"
Delete "$INSTDIRselect.pyd"
Delete "$INSTDIRunicodedata.pyd"
Delete "$INSTDIRuser32.dll"
Delete "$INSTDIRVRA.exe"
Delete "$INSTDIRVRA.exe.manifest"
Delete "$INSTDIRwin32api.pyd"
Delete "$INSTDIRwin32evtlog.pyd"
Delete "$INSTDIRwx._controls_.pyd"
Delete "$INSTDIRwx._core_.pyd"
Delete "$INSTDIRwx._gdi_.pyd"
Delete "$INSTDIRwx._misc_.pyd"
Delete "$INSTDIRwx._windows_.pyd"
Delete "$INSTDIRwxbase30u_net_vc90.dll"
Delete "$INSTDIRwxbase30u_vc90.dll"
Delete "$INSTDIRwxmsw30u_adv_vc90.dll"
Delete "$INSTDIRwxmsw30u_core_vc90.dll"
Delete "$INSTDIRwxmsw30u_html_vc90.dll"
Delete "$INSTDIR\_ctypes.pyd"
Delete "$INSTDIR\_hashlib.pyd"
Delete "$INSTDIR\_socket.pyd"
Delete "$INSTDIR\_ssl.pyd"
Delete "$INSTDIRmplayer.exe"
Delete "$INSTDIRmplayerconfig"
Delete "init.check"
Delete "init.video"
Delete "kamhearing.log"
Delete "player_reg.dll"
Delete "uninstall.exe"
; Remove shortcuts, if any
Delete "$SMPROGRAMSVRA*.*"
Delete "$SMPROGRAMSVRAVRA.lnk"
; Remove directories used
RMDir "$SMPROGRAMSVRA"
RMDir "$INSTDIRmplayer"
RMDir "$INSTDIR"
SectionEnd
;
; 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 $PROGRAMFILESVRA
; Registry key to check for directory (so if you install again, it will
; overwrite the old one automatically)
InstallDirRegKey HKLM "SoftwareVRA" "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 $INSTDIRmplayer
SetOutPath $INSTDIRmplayer
File "mplayerconfig"
SetOutPath $INSTDIR
; Write the installation path into the registry
WriteRegStr HKLM SOFTWAREVRA "Install_Dir" "$INSTDIR"
WriteRegStr HKCU "SoftwareMicrosoftWindowsCurrentVersionRun" Shell "$INSTDIRVRA.exe"
; Write the uninstall keys for Windows
WriteRegStr HKLM "SoftwareMicrosoftWindowsCurrentVersionUninstallVRA" "VRA" "VRA"
WriteRegStr HKLM "SoftwareMicrosoftWindowsCurrentVersionUninstallVRA" "Uninstall_VRA" '"$INSTDIRuninstall.exe"'
WriteRegDWORD HKLM "SoftwareMicrosoftWindowsCurrentVersionUninstallVRA" "NoModify" 1
WriteRegDWORD HKLM "SoftwareMicrosoftWindowsCurrentVersionUninstallVRA" "NoRepair" 1
WriteUninstaller "uninstall.exe"
SectionEnd
; Optional section (can be disabled by the user)
Section "Start Menu Shortcuts"
CreateDirectory "$SMPROGRAMSVRA"
CreateShortCut "$SMPROGRAMSVRAUninstall.lnk" "$INSTDIRuninstall.exe" "" "$INSTDIRuninstall.exe" 0
CreateShortCut "$SMPROGRAMSVRAVRA.lnk" "$INSTDIRVRA.exe" "" "$INSTDIRVRA.exe" 0
SectionEnd
; Optional section (can be disabled by the user)
Section "Desktop Shortcuts" SectionX
SetShellVarContext current
CreateShortCut "$DESKTOPVRA.lnk" "$INSTDIRVRA.exe"
;WriteRegStr HKLM "SoftwareMicrosoftWindows NTCurrentVersionAppCompatFlagslayers" "$INSTDIRVRA.exe" "RUNASADMIN"
SectionEnd
;--------------------------------
; Uninstaller
Section "Uninstall"
; Remove registry keys
DeleteRegKey HKLM "SoftwareMicrosoftWindowsCurrentVersionUninstallVRA"
DeleteRegKey HKLM SOFTWAREVRA
; Remove files and uninstaller
;Delete $INSTDIRexample2.nsi
;Delete $INSTDIRuninstall.exe
Delete "$INSTDIRz2.pyd"
Delete "$INSTDIRMicrosoft.VC90.CRT.manifest"
Delete "$INSTDIRmsvcm90.dll"
Delete "$INSTDIRmsvcp90.dll"
Delete "$INSTDIRmsvcr90.dll"
Delete "$INSTDIRmsvcrt.dll"
Delete "$INSTDIRpycpuid._pycpuid.pyd"
Delete "$INSTDIRpython27.dll"
Delete "$INSTDIRpywintypes27.dll"
Delete "$INSTDIRscreen_left.bmp"
Delete "$INSTDIRselect.pyd"
Delete "$INSTDIRunicodedata.pyd"
Delete "$INSTDIRuser32.dll"
Delete "$INSTDIRVRA.exe"
Delete "$INSTDIRVRA.exe.manifest"
Delete "$INSTDIRwin32api.pyd"
Delete "$INSTDIRwin32evtlog.pyd"
Delete "$INSTDIRwx._controls_.pyd"
Delete "$INSTDIRwx._core_.pyd"
Delete "$INSTDIRwx._gdi_.pyd"
Delete "$INSTDIRwx._misc_.pyd"
Delete "$INSTDIRwx._windows_.pyd"
Delete "$INSTDIRwxbase30u_net_vc90.dll"
Delete "$INSTDIRwxbase30u_vc90.dll"
Delete "$INSTDIRwxmsw30u_adv_vc90.dll"
Delete "$INSTDIRwxmsw30u_core_vc90.dll"
Delete "$INSTDIRwxmsw30u_html_vc90.dll"
Delete "$INSTDIR\_ctypes.pyd"
Delete "$INSTDIR\_hashlib.pyd"
Delete "$INSTDIR\_socket.pyd"
Delete "$INSTDIR\_ssl.pyd"
Delete "$INSTDIRmplayer.exe"
Delete "$INSTDIRmplayerconfig"
Delete "init.check"
Delete "init.video"
Delete "kamhearing.log"
Delete "player_reg.dll"
Delete "uninstall.exe"
; Remove shortcuts, if any
Delete "$SMPROGRAMSVRA*.*"
Delete "$SMPROGRAMSVRAVRA.lnk"
; Remove directories used
RMDir "$SMPROGRAMSVRA"
RMDir "$INSTDIRmplayer"
RMDir "$INSTDIR"
SectionEnd