NSIS (Nullsoft Scriptable Install System) 是一個建立安裝檔非常好用且免費的軟體。
有鑑於資訊的發展日新月異,在進入主題之前,我先註明一下本次筆記內所使用的軟硬體概況:
使用前我試著翻閱中英文的說明,不得不說,這些說明反而讓人望之卻步,正當我苦惱之際,在軟體的介面上,看到「Example scripts」幾個字,順勢點進去之後,我分別開啟 example1 與 example2 這 2 個檔案,觀察一下之後,就直接拿 example2 來改,原因是 example2 把 Uninstaller 的寫法直接放進來,而這正是我需要的,服用的方式,也算容易,用 Notepad++ 打開後,我就用取代的方式,把關鍵字「Example2」換成我要安裝的應用程式名稱,除此之外,還走過下面幾個步驟:
因為要打包的程式,在執行上的需求,我也就把 AccessControl plug-in 安裝進來。
本以為,直接解壓到 NSIS 的安裝目錄就可以,結果…,因為編譯錯誤的關係,我發現要自己去把「i386-ansi」與「i386-unicode」裡面的 AccessControl.dll 分別移到「x86-ansi」與「x86-unicode」(這支 AccessControl.dll 在兩個夾子的名字都一樣,因此,移動的時候,要特別注意別搞錯)。
再經過幾次調校後,就成功打包起來。
可以安裝是一定要的。
然後,可以全權讀取與寫入,所以,程式的執行上,也沒有什麼問題。
最後,跑一下 Uninstaller,確認可以移除檔案、資料夾與捷徑。
最後,考慮到安全性的問題,我就不提供 .nsi 的 Scripts 檔下載,直接貼出來給大家參考,如果也剛好符合您大致上的需求,也可以直接複製去服用。
; DNW.nsi
;
; This script is based on example1.nsi, but it remember the directory,
; has uninstall support and (optionally) installs start menu shortcuts.
;
; It will install DNW.nsi into a directory that the user selects.
;
; See install-shared.nsi for a more robust way of checking for administrator rights.
; See install-per-user.nsi for a file association example.
;--------------------------------
; The name of the installer
Name "DNW"
; The file to write
OutFile "DNWInstaller.exe"
; Request application privileges for Windows Vista and higher
RequestExecutionLevel admin
; Build Unicode installer
Unicode True
; The default installation directory
InstallDir $PROGRAMFILES\DNW
; Registry key to check for directory (so if you install again, it will
; overwrite the old one automatically)
InstallDirRegKey HKLM "Software\DNW" "Install_Dir"
;--------------------------------
; Pages
Page components
Page directory
Page instfiles
UninstPage uninstConfirm
UninstPage instfiles
;--------------------------------
; The stuff to install
Section "DNW (required)"
SectionIn RO
; Set output path to the installation directory.
SetOutPath $INSTDIR
; Put file there
File "DNW.exe"
File "makeData.exe"
; Write the installation path into the registry
WriteRegStr HKLM SOFTWARE\DNW "Install_Dir" "$INSTDIR"
; Write the uninstall keys for Windows
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\DNW" "DisplayName" "DNW"
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\DNW" "UninstallString" '"$INSTDIR\uninstall.exe"'
WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\DNW" "NoModify" 1
WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\DNW" "NoRepair" 1
WriteUninstaller "$INSTDIR\uninstall.exe"
SectionEnd
Section
SetOutPath "$INSTDIR\data"
SetOverwrite on
File /nonfatal /r "$data\*.*"
AccessControl::GrantOnFile "$INSTDIR\data" "(S-1-1-0)" "FullAccess"
AccessControl::GrantOnFile "$INSTDIR\data" "(S-1-5-32-545)" "FullAccess"
# Give all authentificated users (BUILTIN\Users) full access on
# the registry key HKEY_LOCAL_MACHINE\Software\DNW
AccessControl::GrantOnRegKey \
HKLM "Software\DNW\data" "(BU)" "FullAccess"
Pop $0
SectionEnd
; Optional section
Section "Start Menu Shortcuts"
CreateDirectory "$SMPROGRAMS\DNW"
CreateShortcut "$SMPROGRAMS\DNW\Uninstall.lnk" "$INSTDIR\uninstall.exe"
SetOutPath "$INSTDIR"
CreateShortcut "$SMPROGRAMS\DNW\DNW.lnk" "$INSTDIR\DNW.exe"
SectionEnd
; Optional section
Section "Desktop Shortcut"
SetOutPath "$INSTDIR"
CreateShortcut "$DESKTOP\DNW.lnk" "$INSTDIR\DNW.exe"
SectionEnd
;--------------------------------
; Uninstaller
Section "Uninstall"
; Remove registry keys
DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\DNW"
DeleteRegKey HKLM "SOFTWARE\DNW"
; Remove files and uninstaller
Delete "$INSTDIR\*.*"
Delete "$INSTDIR\data\*.*"
; Remove shortcuts, if any
Delete "$SMPROGRAMS\DNW\*.lnk"
Delete "$DESKTOP\DNW.lnk"
; Remove directories
RMDir "$INSTDIR\DNW\data"
RMDir "$INSTDIR\DNW"
RMDir "$INSTDIR"
RMDir /r "$INSTDIR"
SectionEnd