簡體   English   中英

是否可以有條件地將文件/文件夾添加到 NSIS 安裝程序

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

是否可以有條件地向 NSIS 安裝程序添加文件/文件夾和安裝選項? 我的想法是,如果文件夾Foo存在於給定位置,則應將其添加到安裝程序中,並且安裝Foo的選項也應添加到安裝程序中。 但是如果文件夾Foo不存在,NSIS 腳本應該只創建安裝程序,但保留Foo和從其中選擇Foo的選項。

您可以嘗試使用/NONFATAL包含文件。 如果存在,它將被編譯器包含。 在運行時,您可以檢查安裝程序是否能夠提取它。

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

在 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

如果這是不可接受的,您可以使用!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

在 NSIS 3 !if支持 /FileExists 開關:

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

替換依賴於正在運行的服務並且在目標位置是否存在的文件的示例

    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