我创建了一个需要提升权限的 NSIS 安装程序,并且还在用户 AppData
中安装了一些文件目录。只要当前用户是管理员,这就可以正常工作。如果用户是受限用户,则他们必须以管理员身份运行安装程序,并将管理凭据传递给安装程序。除了 NSIS 将文件安装到管理员用户 AppData
之外,这一切都非常有效。在这种情况下的目录。这是因为我一直使用$PROFILE
变量来确定文件的安装位置,但 $PROFILE
返回运行安装程序的用户的路径,而不是登录系统的用户的路径。
需要明确的是,如果 User A
是一个有限帐户并且 User B
是管理员帐户,当我使用 User A
登录计算机时并运行安装程序,传递 User B
的凭据,我只能得到User B
来自 NSIS 的用户名,因此无法安装 User A
所需的文件.
我目前使用的是 NSIS v2.49,我已经有 2 年多没有更新了,所以我认为它已经过时了。在撰写本文时,sourceforge 上的 NSIS 站点已经关闭了几天,因此很难找到该软件的最新副本。如果有人能指出这如何有助于解决这个问题,我愿意升级到 NSIS v3.x。
这是一些非常简单的代码,演示了我的问题。如果您构建此安装程序并在有限用户桌面上运行它,它将演示 3 种不同的尝试来获取当前登录的用户名,但所有 3 种操作都会返回运行安装程序的用户的名称:
Outfile Test.exe
RequestExecutionLevel admin ;Require admin rights on NT6+ (When UAC is turned on)
!include LogicLib.nsh
Function .onInit
UserInfo::GetAccountType
pop $0
${If} $0 != "admin" ;Require admin rights on NT4+
MessageBox mb_iconstop "Administrator rights required!"
SetErrorLevel 740 ;ERROR_ELEVATION_REQUIRED
Quit
${EndIf}
MessageBox MB_OK $PROFILE
System::Call "advapi32::GetUserName(t .r0, *i ${NSIS_MAX_STRLEN} r1) i.r2"
MessageBox MB_OK "User name Test 1: $0"
ReadEnvStr $0 "USERNAME"
MessageBox MB_OK "User name Test 2: $0"
FunctionEnd
Page instfiles
Section
SectionEnd
当以另一个用户身份运行安装程序时,NSIS 安装程序是否无法确定登录用户的用户名?
最佳答案
这就是 UAC 的工作原理。
如果您需要提升权限,那么您正在执行机器/所有用户安装,并且您应该只写入 $ProgramFiles 和 HKLM。将 SetShellVarContext all
添加到 .onInit
函数中,以防止写入用户配置文件。您还应该禁用完成页面上的运行复选框。
如果您的应用程序需要 $AppData 中的数据,则应用程序应在用户第一次运行您的应用程序时将模板数据从 $ProgramFiles、ProgramData (SetShellVarContext all + $AppData) 或 $COMMONFILES 复制到 $AppData。至少从 Windows 2000 开始,这就是 Windows Logo 计划中的一项要求!
您可以在 Certification requirements for Windows desktop apps 中找到这些建议或更旧的Windows Logo requirements文档:
10.3 Your app data, which must be shared among users on the computer, should be stored within ProgramData
10.4 Your app’s data that is exclusive to a specific user and that is not to be shared with other users of the computer, must be stored in Users\%username%\AppData
10.6 Your app must write user data at first run and not during the installation in “per-machine” installations
When the app is installed, there is no correct user location in which to store data. Attempts by an app to modify default association behaviors at a machine level after installation will be unsuccessful. Instead, defaults must be claimed on a per-user level, which prevents multiple users from overwriting each other's defaults.
关于nsis - 以其他用户身份运行时在 NSIS 安装程序中登录用户名 [admin],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48844435/