简体   繁体   English

是否可以有条件地将文件/文件夹添加到 NSIS 安装程序

[英]Is it possible to conditionally add a file/folder to NSIS installer

Is it possible to conditionally add a file/folder and installation option to a NSIS installer?是否可以有条件地向 NSIS 安装程序添加文件/文件夹和安装选项? My idea is that if the folder Foo exists at a given location it should be added to the installer and the option to install Foo should be added to the installer as well.我的想法是,如果文件夹Foo存在于给定位置,则应将其添加到安装程序中,并且安装Foo的选项也应添加到安装程序中。 But if the folder Foo does not exist, the NSIS script should just create the installer but leave Foo and the option to select Foo out of it.但是如果文件夹Foo不存在,NSIS 脚本应该只创建安装程序,但保留Foo和从其中选择Foo的选项。

You can try to include a file with /NONFATAL .您可以尝试使用/NONFATAL包含文件。 If it exists, it will be included by the compiler.如果存在,它将被编译器包含。 In runtime, you can check if installer was able to extract it.在运行时,您可以检查安装程序是否能够提取它。

File /NONFATAL "file.zip"
${If} ${FileExists}  "$OUTDIR\file.zip"
...
${EndIf}

In NSIS 2 File /NONFATAL /R "c:\\foo" is the best you can do without external tools and you need a little hack to hide the section when there are no files:在 NSIS 2 File /NONFATAL /R "c:\\foo"是最好的,你可以在没有外部工具的情况下做,当没有文件时,你需要一点技巧来隐藏该部分:

!include LogicLib.nsh
Page Components
Page InstFiles

Section "Main"
SetOutPath $InstDir
# File "C:\myfiles\myapp.exe"
SectionEnd

Section "Install Foo" SID_FOO
SetOutPath $InstDir
File /NONFATAL /r "C:\myfiles\foo\*.*"
SectionEnd

Function .onInit
SectionGetSize ${SID_FOO} $0
StrCmp $0 0 "" +3
SectionSetFlags ${SID_FOO} 0 ; Force all flags off including the checkmark
SectionSetText ${SID_FOO} "" ; Hide the section because its size is 0
FunctionEnd

If this is unacceptable you can use !system and get a little help from cmd.exe to check if something exists:如果这是不可接受的,您可以使用!system并从 cmd.exe 获得一些帮助以检查是否存在某些内容:

!tempfile INCEXIST
!system 'if exist "C:\myfiles\foo\*.*" echo !define HAVE_FOO > "${INCEXIST}"'
!include "${INCEXIST}"
!delfile "${INCEXIST}"
!ifdef HAVE_FOO
Section "Install Foo"
SetOutPath $InstDir
File /r "C:\myfiles\foo\*.*"
SectionEnd
!endif

In NSIS 3 !if supports a /FileExists switch:在 NSIS 3 !if支持 /FileExists 开关:

!if /FileExists "C:\myfiles\foo\*.*"
Section "Install Foo"
SetOutPath $InstDir
File /r "C:\myfiles\foo\*.*"
SectionEnd
!endif

Example to replace file what depends on running service and exists or not at targget location替换依赖于正在运行的服务并且在目标位置是否存在的文件的示例

    IfFileExists "$SYSDIR\my_file.dll" exist notexist

exist:
ExecWait 'net stop desired_service'

SetOutPath $SYSDIR
SetOverwrite on

File "/oname=$SYSDIR\my_file.dll" "Path to my file\my_file.dll"

ExecWait 'net start desired_service'

notexist:
.....what you want to do if doesn't exists

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM