NSIS检测已安装并卸载旧版本和静默安装.msu文件

本文介绍如何使用NSIS脚本检测计算机上已安装的软件版本,并在新版本安装前静默卸载旧版本。通过注册表读取卸载字符串和版本号,提示用户确认卸载,并执行卸载过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

NSIS检测已安装并卸载旧版本:

Function .onInit
   !insertmacro TIP_WHEN_AMD64_INSTALLER_RUNAT_X86
   ;安装到所有用户下,current 表示安装到当前用户下, all 表示所有用户下。
   SetShellVarContext all
  
   ClearErrors

   SetRegView 64

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

   ReadRegStr $OLD_VER ${PRODUCT_UNINST_ROOT_KEY} ${PRODUCT_UNINST_KEY} "DisplayVersion"
   SetRegView lastused
   MessageBox MB_YESNOCANCEL|MB_ICONQUESTION \
     "检测到本机已经安装了 ${PRODUCT_NAME} $OLD_VER。\
     $\n$\n是否先卸载已安装的版本?" \
       /SD IDYES \
       IDYES uninstall \
       IDNO done
   Abort

  uninstall:
 
   StrCpy $OLD_PATH $UNINSTALL_PROG -11

   ExecWait '"$UNINSTALL_PROG" /S _?=$OLD_PATH' $0
   DetailPrint "uninst.exe returned $0"
   Delete "$UNINSTALL_PROG"
   RMDir $OLD_PATH

 done:
 FunctionEnd

其中:

  1. StrCpy $OLD_PATH $UNINSTALL_PROG -11  相当于获取安装路径:相当于下面两行代码 ("\uninst.exe"一共占11个字符 )

  ${GetParent} $UNINSTALL_PROG $R1
  strcpy $OLD_PATH $R1


  2. ExecWait '"$UNINSTALL_PROG" /S _?=$OLD_PATH' $0  是执行相应的uninst.exe卸载文件。/S 是静默卸载。_?= 作用是将uninst.exe拷贝到临时文件夹( %temp%)之后,再在(_?=后面的)$OLD_PATH文件夹执行的。


可以参考Anders说的:


      Running the uninstaller with _?= is never going to delete the uninstaller .exe.
      When running the uninstaller without _?= what happens is:

  1. uninst.exe is copied to %temp%\~un.exe
  2. uninst.exe does Exec %temp%\~un.exe _?=instdir and quits
  3. %temp%\~un.exe performs uninstall including deleting uninst.exe in $instdir


静默安装.msu文件

 ExecWait 'wusa.exe "$INSTDIR\CamDrive\Windows6.1-KB2921916-x64.msu" /quiet /norestart' $0
 DetailPrint "Windows6.1-KB2921916-x64.msu安装返回了 $0"


参考文章:http://blog.youkuaiyun.com/crazycoder8848/article/details/13703489

                  https://nsis-dev.github.io/NSIS-Forums/html/t-362421.html




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值