在 NSIS 安装程序中包含文件,但不一定安装它们?



尝试从头开始构建自定义NSIS安装程序。

我看到一个File命令,用于包含要安装的文件,但我很难弄清楚如何有选择地安装文件。 我的用例是,我想为我的 .NET Core x86 应用程序、.NET Core x64 应用程序和 .NET 4.6.1 AnyCPU 应用程序创建一个安装程序。

我想我已经想出了如何确定文件应该去哪里......但是在 64 位机器上,我不想安装 32 位文件,反之亦然 对于 32 位操作系统。

File命令建议输出。 如何将所有三个项目的目录包含在安装程序中,但只实际为系统安装正确的文件?

有两种方法可以有条件地安装文件。如果您不需要让用户选择,则可以根据某些条件执行所需的File命令:

!include "LogicLib.nsh"
!include "x64.nsh"
Section
SetOutPath $InstDir
${If} ${RunningX64}
File "myfilesamd64app.exe"
${Else}
File "myfilesx86app.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 "myfilesx86app.exe"
SectionEnd
Section /o "Native 64-bit" SID_AMD64
SetOutPath $InstDir
File "myfilesamd64app.exe"
SectionEnd
Section "AnyCPU" SID_AnyCPU
SetOutPath $InstDir
File "myfilesanycpuapp.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

NSIS提供了几种检查条件的方法,例如StrCmpIntCmp,但最简单的可能是使用LogicLib

例:

!include "LogicLib.nsh"
!include "x64.nsh"
Section
${If} ${RunningX64}
File "that_64bit_file"
${Else}
File "that_32bit_file"
${EndIf}
SectionEnd

最新更新