2009-01-28 86 views
12

我正在使用NSIS爲程序創建安裝程序,檢測此程序是否已安裝的最佳方法是什麼?另外,由於我從autorun.inf運行安裝程序,如果它找到已安裝的副本,是否可以立即退出安裝程序?有一個更好的方法嗎?檢測程序是否已經與NSIS一起安裝

回答

18

這個怎麼樣。 我在放置在一箇舊的NSIS腳本中。

; Check to see if already installed 
    ReadRegStr $R0 HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\<YOUR-APP-NAME>" "UninstallString" 
    IfFileExists $R0 +1 NotInstalled 
    messagebox::show MB_DEFBUTTON4|MB_TOPMOST "<YOUR-APP-NAME>" \ 
    "0,103" \ 
    "<YOUR-APP-NAME> is already installed." \ 
    "Launch Uninstall" "Cancel" 
    Pop $R1 
    StrCmp $R1 2 Quit +1 
    Exec $R0 
Quit: 
    Quit 

NotInstalled: 
+0

您提到啓動卸載。我無法通過使用上面的代碼真正調用我的.nsi腳本中的卸載。你能告訴我,如果我的程序已經安裝,我怎麼能叫卸載? – Jango 2010-06-25 16:21:46

1

這通常是通過讓NSIS在安裝時爲您的產品插入註冊表項來完成的。這是一個簡單的方法來檢測,如果該註冊表項是否存在,如果是這樣,保釋

+3

你可以做任何示例或指針? – 2009-01-28 19:50:57

7

我一直在使用一個稍微複雜的測試,還檢查所安裝的軟件版本:

!define PRODUCT_VERSION "1.2.0" 

!include "WordFunc.nsh" 
    !insertmacro VersionCompare 

Var UNINSTALL_OLD_VERSION 

... 

Section "Core System" CoreSystem 
    StrCmp $UNINSTALL_OLD_VERSION "" core.files 
    ExecWait '$UNINSTALL_OLD_VERSION' 

core.files: 

    ... 
    WriteRegStr HKLM "Software\${PRODUCT_REG_KEY}" "" $INSTDIR 
    WriteRegStr HKLM "Software\${PRODUCT_REG_KEY}" "Version" "${PRODUCT_VERSION}" 
    ... 
SectionEnd 

... 

Function .onInit 
    ;Check earlier installation 
    ClearErrors 
    ReadRegStr $0 HKLM "Software\${PRODUCT_REG_KEY}" "Version" 
    IfErrors init.uninst ; older versions might not have "Version" string set 
    ${VersionCompare} $0 ${PRODUCT_VERSION} $1 
    IntCmp $1 2 init.uninst 
    MessageBox MB_YESNO|MB_ICONQUESTION "${PRODUCT_NAME} version $0 seems to be already installed on your system.$\nWould you like to proceed with the installation of version ${PRODUCT_VERSION}?" \ 
     IDYES init.uninst 
    Quit 

init.uninst: 
    ClearErrors 
    ReadRegStr $0 HKLM "Software\${PRODUCT_REG_KEY}" "" 
    IfErrors init.done 
    StrCpy $UNINSTALL_OLD_VERSION '"$0\uninstall.exe" /S _?=$0' 

init.done: 
FunctionEnd 

當然,你必須填寫的細節,這只是給你一個粗略的骨架。

2

創建卸載程序創建註冊表中的商品名稱填寫之後

!define PRODUCT_UNINST_ROOT_KEY "HKLM" 
!define PRODUCT_UNINST_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANY_NAME} ${PRODUCT_NAME}" 
Section -Post 
    SetShellVarContext current 
    WriteUninstaller "${UNINST_PATH}\uninst.exe" 
    WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayName" "$(^Name)" 

要查看是否安裝了產品做

Function IsProductInstalled 
    ClearErrors 
    ReadRegStr $2 ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayName" 
    StrCmp $2 "" exit 

在你卸載你應該做

Section Uninstall 
    DeleteRegKey ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" 
相關問題