NSIS實際打包.NET程式的專案檔分享(具有偵測.NET和安裝.NET)
NSIS實際打包.NET程式的專案檔分享(具有偵測.NET和安裝.NET)
Code
; 該指令檔使用 HM VNISEdit 指令檔編輯器精靈產生 ; 安裝程式初始定義常量 !define PRODUCT_NAME "JASHWEB_DBBackup" !define PRODUCT_VERSION "1.0" !define PRODUCT_PUBLISHER "JASH, Inc." !define PRODUCT_WEB_SITE "http://www.JASH.com/" !define PRODUCT_DIR_REGKEY "Software\Microsoft\Windows\CurrentVersion\App Paths\JASHWEB_DBBackup.exe" !define PRODUCT_UNINST_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}" !define PRODUCT_UNINST_ROOT_KEY "HKLM" SetCompressor /SOLID lzma ; ------ MUI 現代介面定義 (1.67 版本以上相容) ------ !include "MUI.nsh" ; MUI 預定義常量 !define MUI_ABORTWARNING !define MUI_ICON "Release\JASHWEB-icon_100x100.ico" !define MUI_UNICON "${NSISDIR}\Contrib\Graphics\Icons\modern-uninstall.ico" ; 語言選擇視窗常量設定 !define MUI_LANGDLL_REGISTRY_ROOT "${PRODUCT_UNINST_ROOT_KEY}" !define MUI_LANGDLL_REGISTRY_KEY "${PRODUCT_UNINST_KEY}" !define MUI_LANGDLL_REGISTRY_VALUENAME "NSIS:Language" ; 歡迎頁面 !insertmacro MUI_PAGE_WELCOME ; 授權合約頁面 ;!insertmacro MUI_PAGE_LICENSE "..\..\..\path\to\licence\YourSoftwareLicence.txt" ; 安裝資料夾選擇頁面 !insertmacro MUI_PAGE_DIRECTORY ; 安裝過程頁面 !insertmacro MUI_PAGE_INSTFILES ; 安裝完成頁面 ;!define MUI_FINISHPAGE_RUN "$INSTDIR\JASHWEB_DBBackup.exe" !insertmacro MUI_PAGE_FINISH ; 安裝卸載過程頁面 !insertmacro MUI_UNPAGE_INSTFILES ; 安裝介麵包含的語言設定 !insertmacro MUI_LANGUAGE "English" !insertmacro MUI_LANGUAGE "SimpChinese" !insertmacro MUI_LANGUAGE "TradChinese" ; 安裝預釋放檔案 !insertmacro MUI_RESERVEFILE_LANGDLL !insertmacro MUI_RESERVEFILE_INSTALLOPTIONS ; ------ MUI 現代介面定義結束 ------ ReserveFile "${NSISDIR}\Plugins\splash.dll" ;ReserveFile "c:\path\to\Splash\YourSplash.bmp" ;ReserveFile "c:\path\to\Splash\YourSplashSound.wav" ReserveFile "${NSISDIR}\Plugins\system.dll" ;ReserveFile "c:\path\to\YourMIDI.mid" Name "${PRODUCT_NAME} ${PRODUCT_VERSION}" OutFile "JASHWEB_DBBackup Setup.exe" InstallDir "$PROGRAMFILES\JASHWEB_DBBackup" InstallDirRegKey HKLM "${PRODUCT_UNINST_KEY}" "UninstallString" ShowInstDetails show ShowUnInstDetails show Section "MainSection" SEC01 SetOutPath "$INSTDIR\en" SetOverwrite on File "Release\en\JASHWEB_DBBackup.resources.dll" SetOutPath "$INSTDIR" File "Release\Ionic.Zip.dll" CreateDirectory "$SMPROGRAMS\JASHWEB_DBBackup" CreateShortCut "$SMPROGRAMS\JASHWEB_DBBackup\JASHWEB_DBBackup.lnk" "$INSTDIR\JASHWEB_DBBackup.exe" CreateShortCut "$DESKTOP\JASHWEB_DBBackup.lnk" "$INSTDIR\JASHWEB_DBBackup.exe" File "Release\Mono.Security.dll" File "Release\Npgsql.dll" File "Release\JASHWEB-icon_100x100.ico" File "Release\JASHWEB-icon_100x100.png" File "Release\JASHWEB_DBBackup.exe" File "Release\JASHWEB_DBBackup.pdb" SetOutPath "$INSTDIR\zh-CN" File "Release\zh-CN\JASHWEB_DBBackup.resources.dll" SetOutPath "$INSTDIR\zh-TW" File "Release\zh-TW\JASHWEB_DBBackup.resources.dll" Call GetNetFrameworkVersion Pop $R1 ${If} $R1 < '4.0.30319' SetOverwrite on File "C:\Users\admin\Desktop\Release\dotNetFx40_Full_x86_x64.exe" ${ENDIF} SectionEnd Section -AdditionalIcons SetOutPath $INSTDIR WriteIniStr "$INSTDIR\${PRODUCT_NAME}.url" "InternetShortcut" "URL" "${PRODUCT_WEB_SITE}" CreateShortCut "$SMPROGRAMS\JASHWEB_DBBackup\Website.lnk" "$INSTDIR\${PRODUCT_NAME}.url" CreateShortCut "$SMPROGRAMS\JASHWEB_DBBackup\Uninstall.lnk" "$INSTDIR\uninst.exe" SectionEnd Section -Post WriteUninstaller "$INSTDIR\uninst.exe" WriteRegStr HKLM "${PRODUCT_DIR_REGKEY}" "" "$INSTDIR\JASHWEB_DBBackup.exe" WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayName" "$(^Name)" WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "UninstallString" "$INSTDIR\uninst.exe" WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayIcon" "$INSTDIR\JASHWEB_DBBackup.exe" WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayVersion" "${PRODUCT_VERSION}" WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "URLInfoAbout" "${PRODUCT_WEB_SITE}" WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "Publisher" "${PRODUCT_PUBLISHER}" SectionEnd #-- 依 NSIS 指令檔編輯規則,所有 Function 區段必須放置在 Section 區段之後編寫,以避免安裝程式出現未可預知的問題。--# Function .onInit InitPluginsDir ;File "/oname=$PLUGINSDIR\Splash_YourSplash.bmp" "c:\path\to\Splash\YourSplash.bmp" ;File "/oname=$PLUGINSDIR\Splash_YourSplash.wav" "c:\path\to\Splash\YourSplashSound.wav" ; 使用閃屏外掛程式顯示閃屏 splash::show 1000 "$PLUGINSDIR\Splash_YourSplash" Pop $0 ; $0 返回 '1' 表示使用者提前關閉閃屏, 返回 '0' 表示閃屏正常結束, 返回 '-1' 表示閃屏顯示出錯 ;File "/oname=$PLUGINSDIR\bgm_YourMIDI.mid" "c:\path\to\YourMIDI.mid" ; 開啟音樂檔案 System::Call "winmm.dll::mciSendString(t 'OPEN $PLUGINSDIR\bgm_YourMIDI.mid TYPE SEQUENCER ALIAS BGMUSIC', t .r0, i 130, i 0)" ; 開始播放音樂檔案 System::Call "winmm.dll::mciSendString(t 'PLAY BGMUSIC NOTIFY', t .r0, i 130, i 0)" !insertmacro MUI_LANGDLL_DISPLAY FunctionEnd Function .onGUIEnd ; 停止播放音樂檔案 System::Call "winmm.dll::mciSendString(t 'STOP BGMUSIC',t .r0,i 130,i 0)" ; 關閉音樂檔案 System::Call "winmm.dll::mciSendString(t 'CLOSE BGMUSIC',t .r0,i 130,i 0)" FunctionEnd Section -.NET Call GetNetFrameworkVersion Pop $R1 ${If} $R1 < '4.0.30319' ;當有安裝就要重開機 SetRebootFlag true IfRebootFlag 0 +2 ;當有安裝就要重開機 ;MessageBox MB_OK "Because the installation .NET Framework, must now restart the computer" SetOutPath "$TEMP" SetOverwrite on File "C:\Users\admin\Desktop\Release\dotNetFx40_Full_x86_x64.exe" ExecWait '$TEMP\dotNetFx40_Full_x86_x64.exe /q /norestart /ChainingPackage FullX64Bootstrapper' $R1 Delete "$TEMP\dotNetFx40_Full_x86_x64.exe" ${ENDIF} SectionEnd /****************************** * 以下是安裝程式的卸載部分 * ******************************/ Section Uninstall Delete "$INSTDIR\${PRODUCT_NAME}.url" Delete "$INSTDIR\uninst.exe" Delete "$INSTDIR\zh-TW\JASHWEB_DBBackup.resources.dll" Delete "$INSTDIR\zh-CN\JASHWEB_DBBackup.resources.dll" Delete "$INSTDIR\JASHWEB_DBBackup.pdb" Delete "$INSTDIR\JASHWEB_DBBackup.exe" Delete "$INSTDIR\JASHWEB-icon_100x100.png" Delete "$INSTDIR\JASHWEB-icon_100x100.ico" &n bsp; Delete "$INSTDIR\Npgsql.dll" Delete "$INSTDIR\Mono.Security.dll" Delete "$INSTDIR\Ionic.Zip.dll" Delete "$INSTDIR\en\JASHWEB_DBBackup.resources.dll" Delete "$INSTDIR\dotNetFx40_Full_x86_x64.exe" Delete "$INSTDIR\Uninstall.lnk" Delete "$INSTDIR\Website.lnk" Delete "$INSTDIR\JASHWEB_DBBackup.lnk" Delete "$DESKTOP\JASHWEB_DBBackup.lnk" SetShellVarContext all Delete "$SMPROGRAMS\JASHWEB_DBBackup\Uninstall.lnk" Delete "$SMPROGRAMS\JASHWEB_DBBackup\Website.lnk" Delete "$DESKTOP\JASHWEB_DBBackup.lnk" Delete "$SMPROGRAMS\JASHWEB_DBBackup\JASHWEB_DBBackup.lnk" RMDir "$SMPROGRAMS\JASHWEB_DBBackup" RMDir "$INSTDIR\zh-TW" RMDir "$INSTDIR\zh-CN" RMDir "$INSTDIR\en" RMDir "$INSTDIR" DeleteRegKey ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" DeleteRegKey HKLM "${PRODUCT_DIR_REGKEY}" SetAutoClose true SectionEnd 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 #-- 依 NSIS 指令檔編輯規則,所有 Function 區段必須放置在 Section 區段之後編寫,以避免安裝程式出現未可預知的問題。--# Function un.onInit !insertmacro MUI_UNGETLANGUAGE MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2 "你確實要完全移除 $(^Name) ,及其所有的元件?" IDYES +2 Abort FunctionEnd Function un.onUninstSuccess HideWindow MessageBox MB_ICONINFORMATION|MB_OK "$(^Name) 已成功地從你的電腦移除。" FunctionEnd