【问题标题】:Include files in NSIS installer, but don't necessarily install them?在 NSIS 安装程序中包含文件,但不一定要安装它们?
【发布时间】:2018-09-11 16:36:04
【问题描述】:
尝试从头开始构建自定义 NSIS 安装程序。
我看到一个 File 命令包含您要安装的文件,但我很难弄清楚如何有选择地安装文件。我的用例是,我想为我的 .NET Core x86 应用程序、我的 .NET Core x64 应用程序和我的 .NET 4.6.1 AnyCpu 应用程序创建一个安装程序。
我想我已经知道如何确定文件应该放在哪里...但是在 64 位机器上,我不想安装 32 位文件,反之亦然32 位操作系统。
File 命令建议输出。如何将所有三个项目的目录都包含到安装程序中,但只实际安装系统的正确文件?
【问题讨论】:
标签:
windows
installation
nsis
【解决方案1】:
有两种方法可以有条件地安装文件。如果您不需要让用户选择,您可以根据某些条件执行所需的File 命令:
!include "LogicLib.nsh"
!include "x64.nsh"
Section
SetOutPath $InstDir
${If} ${RunningX64}
File "myfiles\amd64\app.exe"
${Else}
File "myfiles\x86\app.exe"
${EndIf}
SectionEnd
如果您希望用户能够选择,您可以将File 命令放在不同的部分:
!include "LogicLib.nsh"
!include "x64.nsh"
!include "Sections.nsh"
Page Components
Page Directory
Page InstFiles
Section /o "Native 32-bit" SID_x86
SetOutPath $InstDir
File "myfiles\x86\app.exe"
SectionEnd
Section /o "Native 64-bit" SID_AMD64
SetOutPath $InstDir
File "myfiles\amd64\app.exe"
SectionEnd
Section "AnyCPU" SID_AnyCPU
SetOutPath $InstDir
File "myfiles\anycpu\app.exe"
SectionEnd
Var CPUCurrSel
Function .onInit
StrCpy $CPUCurrSel ${SID_AnyCPU} ; The default
${If} ${RunningX64}
!insertmacro RemoveSection ${SID_x86}
${Else}
!insertmacro RemoveSection ${SID_AMD64}
${EndIf}
FunctionEnd
Function .onSelChange
!insertmacro StartRadioButtons $CPUCurrSel
!insertmacro RadioButton ${SID_x86}
!insertmacro RadioButton ${SID_AMD64}
!insertmacro RadioButton ${SID_AnyCPU}
!insertmacro EndRadioButtons
FunctionEnd
【解决方案2】:
NSIS 提供了几种检查条件的方法,例如 StrCmp 或 IntCmp,但最简单的可能是使用 LogicLib 库
示例:
!include "LogicLib.nsh"
!include "x64.nsh"
Section
${If} ${RunningX64}
File "that_64bit_file"
${Else}
File "that_32bit_file"
${EndIf}
SectionEnd