2016-03-08 47 views
0

Noob問題!我需要設置我的安裝目錄

所以到目前爲止,我寫了這個代碼

; NSIS packaging/install script 
; Docs: http://nsis.sourceforge.net/Docs/Contents.html 

!include LogicLib.nsh 
!include nsDialogs.nsh 

; -------------------------------- 
; Variables 
; -------------------------------- 

!define dest "{{dest}}" 
!define src "{{src}}" 
!define name "{{name}}" 
!define productName "{{productName}}" 
!define author "{{author}}" 
!define version "{{version}}" 
!define icon "{{icon}}" 
!define setupIcon "{{setupIcon}}" 
!define banner "{{banner}}" 

!define exec "{{productName}}.exe" 

!define regkey "Software\${productName}" 
!define uninstkey "Software\Microsoft\Windows\CurrentVersion\Uninstall\${productName}" 

!define uninstaller "uninstall.exe" 

; -------------------------------- 
; Installation 
; -------------------------------- 

Unicode true 
SetCompressor /SOLID lzma 

Name "${productName}" 
Icon "${setupIcon}" 
OutFile "${dest}" 
;InstallDir "$PROGRAMFILES\${productName}" 
InstallDir $PROGRAMFILES\WhapTk 
DirText "This will install WhapTk on your computer. Choose a directory" 
InstallDirRegKey HKLM "${regkey}" "" 

RequestExecutionLevel admin 
CRCCheck on 
SilentInstall normal 

XPStyle on 
ShowInstDetails nevershow 
AutoCloseWindow false 
WindowIcon off 

Caption "${productName} Setup" 
; Don't add sub-captions to title bar 
SubCaption 3 " " 
SubCaption 4 " " 

Page custom welcome 
Page instfiles 

Var Image 
Var ImageHandle 

Function .onInit 

    ; Extract banner image for welcome page 
    InitPluginsDir 
    ReserveFile "${banner}" 
    File /oname=$PLUGINSDIR\banner.bmp "${banner}" 

FunctionEnd 

; Custom welcome page 
Function welcome 

    nsDialogs::Create 1018 

    ${NSD_CreateLabel} 185 1u 210 100% "Welcome to ${productName} version ${version} installer.$\r$\n$\r$\nClick install to begin." 

    ;InstallDir $PROGRAMFILES\WhapTk 

    ${NSD_CreateBitmap} 0 0 170 210 "" 
    Pop $Image 
    ${NSD_SetImage} $Image $PLUGINSDIR\banner.bmp $ImageHandle 

    SetOutPath $INSTDIR 

    nsDialogs::Show 

    ${NSD_FreeImage} $ImageHandle 

FunctionEnd 

; Installation declarations 
Section "Install" 

    WriteRegStr HKLM "${regkey}" "Install_Dir" "$INSTDIR" 
    WriteRegStr HKLM "${uninstkey}" "DisplayName" "${productName}" 
    WriteRegStr HKLM "${uninstkey}" "DisplayIcon" '"$INSTDIR\icon.ico"' 
    WriteRegStr HKLM "${uninstkey}" "UninstallString" '"$INSTDIR\${uninstaller}"' 
    WriteRegStr HKLM "${uninstkey}" "Publisher" "${author}" 
    WriteRegStr HKLM "${uninstkey}" "DisplayVersion" "${version}" 

    ; Remove all application files copied by previous installation 
    RMDir /r "$INSTDIR" 


    ; Include all files from /build directory 
    File /r "${src}\*" 

    ; Create start menu shortcut 
    SetShellVarContext all 
    CreateShortCut "$SMPROGRAMS\${productName}.lnk" "$INSTDIR\${exec}" "" "$INSTDIR\icon.ico" 
    ; Create desktop shortcut 
    CreateShortCut "$DESKTOP\${productName}.lnk" "$INSTDIR\${exec}" "" "$INSTDIR\icon.ico" 

    WriteUninstaller "${uninstaller}" 

SectionEnd 

; -------------------------------- 
; Uninstaller 
; -------------------------------- 

ShowUninstDetails nevershow 

UninstallCaption "Uninstall ${productName}" 
UninstallText "Don't like ${productName} anymore? Hit uninstall button." 
UninstallIcon "${icon}" 

UninstPage custom un.confirm un.confirmOnLeave 
UninstPage instfiles 

Var RemoveAppDataCheckbox 
Var RemoveAppDataCheckbox_State 

; Custom uninstall confirm page 
Function un.confirm 

    nsDialogs::Create 1018 

    ${NSD_CreateLabel} 1u 1u 100% 24u "If you really want to remove ${productName} from your computer press uninstall button." 

    ${NSD_CreateCheckbox} 1u 35u 100% 10u "Remove also my ${productName} personal data" 
    Pop $RemoveAppDataCheckbox 

    nsDialogs::Show 

FunctionEnd 

Function un.confirmOnLeave 

    ; Save checkbox state on page leave 
    ${NSD_GetState} $RemoveAppDataCheckbox $RemoveAppDataCheckbox_State 

FunctionEnd 

; Uninstall declarations 
Section "Uninstall" 

    DeleteRegKey HKLM "${uninstkey}" 
    DeleteRegKey HKLM "${regkey}" 

    SetShellVarContext all 
    Delete "$SMPROGRAMS\${productName}.lnk" 
    ; Remove desktop shortcut 
    Delete "$DESKTOP\${productName}.lnk" 
    ; Remove whole directory from Program Files 
    RMDir /r "$INSTDIR" 

    ; Remove also appData directory generated by your app if user checked this option 
    ${If} $RemoveAppDataCheckbox_State == ${BST_CHECKED} 
     RMDir /r "$APPDATA\${productName}" 
    ${EndIf} 

SectionEnd 

這些是學校的電腦,所以我希望用戶能夠安裝的exe,他們選擇的文件夾中。 (學校限制了C盤) 我試過

;InstallDir "$PROGRAMFILES\${productName}" 
InstallDir $PROGRAMFILES\WhapTk 

但是拒絕工作:((安裝程序工作正常,它只是我不能選擇我的目錄) 請告訴我,我做錯了什麼! 在此先感謝

回答

0

您需要爲選擇對話框中指定其他Page

Page directory 
Page instfiles 

請記住,聲明這些頁面的順序是非常重要的,通常instfiles最後。

+0

好的,謝謝!我已經得到了「選擇目錄」頁面!但不幸的是,我選擇它安裝的地方沒有得到該文件。無論我做什麼,它都會下載到Program Files文件中! –

+0

在你的'File'指令前加上'SetOutPath $ INSTDIR' – idleberg

0

InstallDir屬性在安裝程序啓動之前初始化$InstDir變量,然後執行任何代碼。

如果您想允許最終用戶更改安裝目錄,那麼您通常會添加一個目錄頁面,但您也可以在自定義頁面或InstFiles頁面之前的任何其他位置執行此操作;只需使用像StrCpy $InstDir "c:\some\new\path"這樣的東西。

在使用File指令來提取文件之前,您必須使用SetOutPath指令。它創建目錄並設置File說明的目的地。

的最小安裝程序可能會是這個樣子:

OutFile "MySetup.exe" 
InstallDir "$ProgramFiles\MyApp" ; $InstDir default value 
RequestExecutionLevel Admin 

Page Directory ; This page might change $InstDir 
Page InstFiles 

Section 
SetOutPath "$InstDir" ; This basically does StrCpy $OutDir $InstDir + CreateDirectory $OutDir 
File "MyApp.exe" ; This will extract as "$OutDir\MyApp.exe" 
SectionEnd 

您的示例代碼調用您的自定義歡迎頁SetOutPath在這一點$InstDir只是有從InstallDir的價值和你永遠之後再打電話SetOutPath

+0

好的!所以,這工作,但一個問題...當我將它添加到我的代碼,我設置它的任何目錄被自動擦除..(當我將它設置到我的Program Files XD時,給了我一個恐慌)有沒有反正它在用戶指定的目錄中創建一個新文件夾,然後從那裏安裝它? –

+0

您的代碼中有RMDir/R。大多數安裝程序只會刪除卸載程序中的內容。在代碼中添加「MessageBox MB_OK $ InstDir」幾個位置並觀察安裝程序中某些點的路徑更改(一個in.onInit,一個位於第一個部分等),這可能是一個好主意。 – Anders

+0

因此,如果我刪除刪除目錄中的所有文件的代碼,它會自動添加一個文件夾(在我的情況下是「WhapTk」,它將程序文件放在那裏嗎?) –