根据MultiUser.nsh
的宏,$INSTDIR
将根据$MultiUser.InstallMode
的值设置为以下路径之一:
"$LOCALAPPDATA\${MULTIUSER_INSTALLMODE_INSTDIR}"
"$PROGRAMFILES\${MULTIUSER_INSTALLMODE_INSTDIR}"
基本上,宏会将$MULTIUSER_INSTALLMODE_INSTDIR
附加到$LOCALAPPDATA
或$PROGRAMFILES
,并将其用作默认安装路径。
我需要的是仅适用于CurrentUser安装模式的路径,即:
"$LOCALAPPDATA\Programs\${MULTIUSER_INSTALLMODE_INSTDIR}"
我怎样才能做到这一点?
鉴于用户有机会选择installMode,必须在做出此选择后设置默认的$INSTDIR
值。
答案 0 :(得分:0)
您可以使用MULTIUSER_INSTALLMODE_FUNCTION回调函数定义进行更改:
Name "MultiUser test"
!define MULTIUSER_INSTALLMODE_INSTDIR "$(^Name)"
!define MULTIUSER_EXECUTIONLEVEL Highest
!define MULTIUSER_INSTALLMODE_FUNCTION onMultiUserModeChanged
!define MULTIUSER_MUI
!include MultiUser.nsh
!include MUI2.nsh
!include LogicLib.nsh
!insertmacro MULTIUSER_PAGE_INSTALLMODE
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES
!insertmacro MUI_LANGUAGE English
Function .onInit
!insertmacro MULTIUSER_INIT
FunctionEnd
Function un.onInit
!insertmacro MULTIUSER_UNINIT
FunctionEnd
Function onMultiUserModeChanged
${If} $MultiUser.InstallMode == "CurrentUser"
StrCpy $InstDir "$LocalAppdata\Programs\${MULTIUSER_INSTALLMODE_INSTDIR}"
${EndIf}
FunctionEnd
Section
MessageBox MB_OK $InstDir
Quit ; This is just a demo
SetOutPath $InstDir
WriteUninstaller "$InstDir\Uninst.exe"
SectionEnd
Section Uninstall
Delete "$InstDir\Uninst.exe"
RMDir $InstDir
SectionEnd
甚至更好,使用UserProgramFiles已知文件夹(如果可用):
!macro GetUserProgramFiles outvar
!define /IfNDef KF_FLAG_CREATE 0x00008000
!define /IfNDef FOLDERID_UserProgramFiles {5CD7AEE2-2219-4A67-B85D-6C9CE15660CB}
System::Store S
System::Call 'SHELL32::SHGetKnownFolderPath(g "${FOLDERID_UserProgramFiles}", i ${KF_FLAG_CREATE}, p 0, *p .r2)i.r1' ; This will only work on Win7+
${If} $1 == 0
System::Call '*$2(&w${NSIS_MAX_STRLEN} .s)'
System::Call 'OLE32::CoTaskMemFree(p r2)'
${Else}
Push "$LocalAppData\Programs"
${EndIf}
System::Store L
Pop ${outvar}
!macroend
Function onMultiUserModeChanged
${If} $MultiUser.InstallMode == "CurrentUser"
!insertmacro GetUserProgramFiles $InstDir
StrCpy $InstDir "$InstDir\${MULTIUSER_INSTALLMODE_INSTDIR}"
${EndIf}
FunctionEnd