installation - NSIS,安装时检测目录是否存在

标签 installation nsis

我有安装器,支持选择安装目录。我想检测给定的文件夹是否存在以及它是否为空。如果它不为空,则显示警告消息框,然后删除其所有内容并将程序安装到该文件夹​​中。唯一的问题是进入正确的代码部分,在那里我可以获得用户在安装过程中提供的安装文件夹,我可以处理其余部分。

感谢您的任何建议。

最佳答案

通常你会检查目录是否存在:

Outfile "$%Temp%\Test.exe"
RequestExecutionLevel user
InstallDir "$Documents\Test"

!include LogicLib.nsh

Page Directory "" "" DirLeave
Page InstFiles

Function DirLeave
${If} ${FileExists} "$InstDir\*"
    MessageBox MB_YESNO `"$InstDir" already exists, delete it's content and continue installing?` IDYES yep
    Abort
yep:
    RMDir /r "$InstDir"
${EndIf}
FunctionEnd

Section
SetOutPath $InstDir
File myfile.ext
SectionEnd

如果目录存在但为空,这也会显示消息。要解决这个问题,您需要一些自定义检测:

!macro _IsNonEmptyDirectory _a _b _t _f
!insertmacro _LOGICLIB_TEMP
!insertmacro _IncreaseCounter
Push $0
FindFirst $0 $_LOGICLIB_TEMP "${_b}\*"
_IsNonEmptyDirectory_loop${LOGICLIB_COUNTER}:
    StrCmp "" $_LOGICLIB_TEMP _IsNonEmptyDirectory_done${LOGICLIB_COUNTER}
    StrCmp "." $_LOGICLIB_TEMP +2
    StrCmp ".." $_LOGICLIB_TEMP 0 _IsNonEmptyDirectory_done${LOGICLIB_COUNTER}
    FindNext $0 $_LOGICLIB_TEMP
    Goto _IsNonEmptyDirectory_loop${LOGICLIB_COUNTER}
_IsNonEmptyDirectory_done${LOGICLIB_COUNTER}:
FindClose $0
Pop $0
!insertmacro _!= "" $_LOGICLIB_TEMP `${_t}` `${_f}`
!macroend
!define IsNonEmptyDirectory `"" IsNonEmptyDirectory`

Function DirLeave
${If} ${IsNonEmptyDirectory} "$InstDir"
    MessageBox MB_YESNO `"$InstDir" already exists, delete it's content and continue installing?` IDYES yep
    Abort
yep:
    RMDir /r "$InstDir"
${EndIf}
FunctionEnd

关于installation - NSIS,安装时检测目录是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29560977/

相关文章:

在 Windows 上安装更新时,Electron 自动更新失败

windows - 如何创建 Windows 8 风格的安装程序

windows - 如何在 NSIS 安装程序的完成页面上添加桌面快捷方式选项?

python-3.x - 使用 git+ssh 进行 Pip 安装不适用于 python 包

android - APK 文件作为存档类型存储在设备的下载文件夹中

Python-pptx for Python 3.5.1

nsis - 如何读取注册表中的子项

nsis - 使用 NSIS 嵌入其他安装程序

c# - 是否有支持 .NET4 的第三方打包工具?

linux - 使用共享对象和符号链接(symbolic link)构建安装包