如果未选中部分,NSIS 会执行操作

NSIS do things if section is not checked

我创建了一个运行良好的 NSIS 安装程序。现在我想添加另一个名为 "install as update" 的部分,它只会在未选中时执行操作。

为什么: 安装完整版后,会覆盖某些包含软件激活码的文件。

我可以不这样做,并创建一个名为 "install full version" 的部分,但这不太有意义。

Section /o "Install as update" SecUpdate  
    *if(checked == false){
        SetOutPath "$INSTDIR\data"
        File "data\ConfigFile.xml"
        File "..."
        File "..."
        File "..."
        File "..."
        File "..."
        File "..."
        File "..."
    *}
SectionEnd

*这两行代表我想做的事情。

如果某个部分未选中,则无论您做什么,其中的代码都不会执行,因此您必须将代码放在其他地方。隐藏部分是一个很好的解决方案:

!include LogicLib.nsh
!include Sections.nsh

Page Components
Page InstFiles

Section "Program files"
SectionIn RO
;SetOutPath ...
;File ...
SectionEnd

Section /o "Install as update" SID_UPDATE
SectionEnd

Section -OverwriteActivation SID_OWACTIVATION
SetOutPath "$INSTDIR\data"
File "whatever.xml"
SectionEnd

Function .onSelChange
${If} ${SectionIsSelected} ${SID_UPDATE}
    !insertmacro UnselectSection ${SID_OWACTIVATION}
${Else}
    !insertmacro SelectSection ${SID_OWACTIVATION}
${EndIf}
FunctionEnd