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

问题描述 投票:0回答:2

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

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

我想我已经弄明白了如何确定文件的位置...但是在64位机器上,我不想安装32位文件,反之亦然32位文件OS。

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

windows installation installer nsis
2个回答
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

1
投票

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

例:

!include "LogicLib.nsh"
!include "x64.nsh"

Section
  ${If} ${RunningX64}
      File "that_64bit_file"
  ${Else}
      File "that_32bit_file"
  ${EndIf}
SectionEnd
© www.soinside.com 2019 - 2024. All rights reserved.