NSIS打包

活著 就好發表於2020-10-22

1、安裝完畢,開啟NIS Edit,點選檔案,選擇新建指令碼嚮導

2、填寫應該寫的,沒有可以為空

3、設定安裝語言為“simple chinese”(簡體中文),設定安裝程式圖示(xxx.ico),其餘預設即可

4、填寫應用程式安裝的預設路徑,沒有授權檔案的話置空,有的話百度

5、如圖所示

接上一步,點選樹形目錄,這裡選擇要釋出的exe程式所在的目錄,

並選中"包含子目錄"以及"單獨新增每個檔案" ,其餘預設

6、預設即可,具體要求自行百度

7、預設即可(當然也可新增一些自定義的自述,如漢化說明等等)

8、在圖中只需要修改自定義的解除安裝程式圖示就可以了,其他預設,選擇下一步

9、選擇“儲存指令碼”,“轉換檔案路徑到相對路徑”,“編譯指令碼”,然後用NIS Edit進行編譯,執行打包過程,最後生成成功,點選Setup.exe執行即可安裝。

注意:如果想在程式安裝的時候需要進行.NET FRAMEWORK的判斷及靜默安裝,則需要在nsi指令碼中新增如下程式碼:

Function GetNetFrameworkVersion

;獲取.Net Framework版本支援

Push $1

Push $0

ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full" "Install"

ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full" "Version"

StrCmp $0 1 KnowNetFrameworkVersion +1

ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5" "Install"

ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5" "Version"

StrCmp $0 1 KnowNetFrameworkVersion +1

ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.0\Setup" "InstallSuccess"

ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.0\Setup" "Version"

StrCmp $0 1 KnowNetFrameworkVersion +1

ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v2.0.50727" "Install"

ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v2.0.50727" "Version"

StrCmp $1 "" +1 +2

StrCpy $1 "2.0.50727.832"

StrCmp $0 1 KnowNetFrameworkVersion +1

ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v1.1.4322" "Install"

ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v1.1.4322" "Version"

StrCmp $1 "" +1 +2

StrCpy $1 "1.1.4322.573"

StrCmp $0 1 KnowNetFrameworkVersion +1

ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\.NETFramework\policy\v1.0" "Install"

ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\.NETFramework\policy\v1.0" "Version"

StrCmp $1 "" +1 +2

StrCpy $1 "1.0.3705.0"

StrCmp $0 1 KnowNetFrameworkVersion +1

StrCpy $1 "not .NetFramework"

KnowNetFrameworkVersion:

Pop $0

Exch $1

FunctionEnd

其他:安裝程式是判斷當前電腦是否已安裝程式,是否解除安裝

Var UNINSTALL_PROG

Var OLD_VER

Var OLD_PATH

Function .onInit

ClearErrors

ReadRegStr $UNINSTALL_PROG ${PRODUCT_UNINST_ROOT_KEY} ${PRODUCT_UNINST_KEY} "UninstallString"

IfErrors done


ReadRegStr $OLD_VER ${PRODUCT_UNINST_ROOT_KEY} ${PRODUCT_UNINST_KEY} "DisplayVersion"

MessageBox MB_YESNOCANCEL|MB_ICONQUESTION \

"檢測到本機已經安裝了 ${PRODUCT_NAME} $OLD_VER。\

$\n$\n是否先解除安裝已安裝的版本?" \

/SD IDYES \

IDYES uninstall \

IDNO done

Abort


uninstall:

StrCpy $OLD_PATH $UNINSTALL_PROG -10


ExecWait '"$UNINSTALL_PROG" /S _?=$OLD_PATH' $0

DetailPrint "uninst.exe returned $0"

Delete "$UNINSTALL_PROG"

RMDir $OLD_PATH



done:

FunctionEnd

以上程式碼可以放在Section "主程式必選" SEC01之前,然後在進行編譯指令碼。

其他:   如果要執行批處理,且需要在安裝路徑執行,可新增以下程式碼:

Section "生成機器碼"

#SetOutPath "$INSTDIR\runHID.bat"

#nsExec::Exec "$INSTDIR\runHID.bat"

SetOutPath '$INSTDIR'

ExecWait '$INSTDIR/runHID.bat'

SectionEnd