windows 检测程序是否已与 NSIS 一起安装

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/488717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:59:40  来源:igfitidea点击:

Detecting if a program is already installed with NSIS

windowsnsisautorun

提问by Abdullah Jibaly

I'm using NSIS to create an installer for a program, what is the best way to detect if this program is already installed? Also, since I'm running the installer from the autorun.inf, can I immediately quit the installer if it locates an installed copy? Is there a better way to do this?

我正在使用 NSIS 为程序创建安装程序,检测该程序是否已安装的最佳方法是什么?另外,由于我从 autorun.inf 运行安装程序,如果它找到已安装的副本,我可以立即退出安装程序吗?有一个更好的方法吗?

回答by Magnus Johansson

How about this. I had this in an old NSIS script laying around.

这个怎么样。我在一个旧的 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:

回答by David Hanak

I've been using a slightly more sophisticated test which also checks the version of the installed software:

我一直在使用稍微复杂的测试,它还会检查已安装软件的版本:

!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 
!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)"
HKLM "Software${PRODUCT_REG_KEY}" "Version" IfErrors init.uninst ; older versions might not have "Version" string set ${VersionCompare}
Function IsProductInstalled
  ClearErrors
  ReadRegStr  ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayName"  
  StrCmp  "" exit
${PRODUCT_VERSION} IntCmp 2 init.uninst MessageBox MB_YESNO|MB_ICONQUESTION "${PRODUCT_NAME} version
Section Uninstall
    DeleteRegKey ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}"
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 ##代码## HKLM "Software${PRODUCT_REG_KEY}" "" IfErrors init.done StrCpy $UNINSTALL_OLD_VERSION '"##代码##\uninstall.exe" /S _?=##代码##' init.done: FunctionEnd

You of course have to fill in the details, this only gives you a rough skeleton.

你当然必须填写细节,这只会给你一个粗略的骨架。

回答by Cherian

After creating your uninstaller create a product name entry in registry

创建卸载程序后,在注册表中创建一个产品名称条目

##代码##

To see if the product is installed do

要查看产品是否已安装,请执行

##代码##

In your uninstall you should be doing

在你的卸载你应该做

##代码##

回答by Kevin

This is generally done by having NSIS insert a registry key for your product when it installs. It is then an easy manner to detect if that registry key is present and if so, bail

这通常是通过让 NSIS 在安装时为您的产品插入注册表项来完成的。这是一种检测该注册表项是否存在的简单方法,如果存在,则保释