以管理员身份运行时获取当前登录用户 [英] Getting currently logged on user when running as admin

查看:81
本文介绍了以管理员身份运行时获取当前登录用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用NSIS编写了一个安装程序,我需要它在标准位置(例如C:/Program Files/App Name/)中安装一些文件(DLL等).我还需要在当前用户的应用程序数据"目录中安装文件.问题是,当用户不是Vista上的管理员时,我需要提升特权,并且这样做时环境变量会更改,因此当前用户现在似乎是admin用户,而最终我将安装在Admin用户的目录中,而不是实际用户.启动安装程序时,您必须先提升权限,这样我才能抓住用户名然后再提升.当我以管理员身份运行安装程序时,有什么合理的方法可以确定实际用户是谁?

I've written an installer using NSIS and I need it to install some files (DLLs etc.) in a standard location like C:/Program Files/App Name/. I also need to install files in the current user's Application Data directory. The problem is that when the user is not an admin on Vista I need to elevate privileges and in doing so the environment variables change so the current user now appears to be the admin user and I end up installing in the Admin user's directory instead of the actual user. You have to elevate when you start the installer so I can't grab the username and then elevate. Is there some reasonable way to figure out who the actual user is when I'm running an installer as an admin?

不幸的是,第一次运行时不能复制数据.没有我要写到用户目录的东西,该应用程序将无法运行,因为它是MS Word模板.没有模板,我的东西就不会运行,因此我必须在安装时编写模板,并且Word要求模板位于用户的AppData目录中.

Unfortunately having the data copied on first run is not an option. The app won't work without the thing I'm writing to the user's directory because it's a MS Word template. My stuff WOULD NOT RUN without the template, so I MUST write the template on install and Word requires the template to be in the user's AppData dir.

推荐答案

我所有的NSIS安装程序都使用以下UAC插件: http://nsis.sourceforge.net/UAC_plug-in .

All of my NSIS installers use this UAC plug-in: http://nsis.sourceforge.net/UAC_plug-in.

启动安装程序后,您可以立即提升为管理员.这会产生完成所有工作的安装程序的第二个实例.但是您可以调用代码段,这些代码段将以启动安装程序的用户的身份在原始过程中运行.

You can elevate to admin as soon as you launch the installer. This spawns a second instance of your installer that does all of the work. But you can make calls to code segments that will run in the original process as the user that launched the installer.

因此,您可以有一个类似于以下内容的安装程序部分:

So you could have an installer section like so:

Section "Install My Program" SecMain
    ...    
    # Install files to common folders
    ...
    !insertmacro UAC.CallFunctionAsUser CopyUserWordTemplate
    ...
SectionEnd

Function CopyUserWordTemplate
    SetOutPath $LOCALAPPDATA
    File "MyWordTemplate.dot"
FunctionEnd

这篇关于以管理员身份运行时获取当前登录用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆