如何在NSIS中切换需要和不需要管理员权限?

3
我想创建一个双重安装程序,可以将应用程序安装为便携版或普通版本。
对于便携版,我不想要求管理员权限。对于普通版本,我需要管理员权限来将应用程序添加到开始菜单和其他位置。
有没有一种方法在启动实际安装时提示用户获取管理员权限?也许可以使用插件?我正在寻找类似于
中的RequestExecutionLevel admin的内容。
1个回答

1

RequestExecutionLevel highest会强制管理员组成员提升权限,而普通用户可以在没有UAC交互的情况下运行它。这个例子不会为您提升权限,因为这样做很棘手,UAC在某些情况下会出现问题,需要更多的代码才能正确地实现...

RequestExecutionLevel highest
Var InstMode

!include nsDialogs.nsh
!include Sections.nsh
!include LogicLib.nsh
Page Custom InstallModePageInit InstallModePageLeave
Page InstFiles

Section "StartMenu shortcuts" SEC_SM
; CreateShortcut ...
SectionEnd
Section "" SEC_UNINST
; WriteUninstaller & registry
SectionEnd

Function InstallModePageInit
nsDialogs::Create 1018
Pop $0

${NSD_CreateRadioButton} 20u 30u 100% 12u "Normal install"
Pop $1
${NSD_CreateRadioButton} 20u 50u 100% 12u "Portable install"
Pop $2

${If} $InstMode = 0
    ${NSD_Check} $1
${Else}
    ${NSD_Check} $2
${EndIf}
nsDialogs::Show
FunctionEnd

Function InstallModePageLeave
${NSD_GetState} $2 $InstMode
${If} $InstMode = 0
    !insertmacro SelectSection ${SEC_SM}
    !insertmacro SelectSection ${SEC_UNINST}
    UserInfo::GetAccountType
    Pop $0
    ${If} $0 != "Admin"
        MessageBox mb_iconstop "Administrator privileges required, please restart installer to continue..."
        Abort
    ${EndIf}
${Else}
    !insertmacro UnselectSection ${SEC_SM}
    !insertmacro UnselectSection ${SEC_UNINST}
${EndIf}
FunctionEnd

选择“最高权限”将强制以管理员身份安装,如果用户是管理员的话。我认为原始问题想要的是管理员在安装时能够选择是否进行系统安装。 - teeks99
最高权限将允许管理员用户进行选择。他们将被强制提升到UAC,但非管理员安装应该能够正常工作。 - Anders

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接