简体   繁体   English

获取登录的用户名以激活进程

[英]Getting the logged in user name to activate a process with

I'm using C# with .NET 3.5 我在C#和.NET 3.5中使用

My goal here is to take the logged in user name and simply start a process under this user name. 我的目标是获取登录的用户名,然后简单地以该用户名启动进程。 Normally a simple Process.Start(pathToProgram) would help here, but the problem is that I'm calling this line from an installer class, meaning the msiexec is currently working and the user name i get is SYSTEM instead of the actual user who currently logged in to windows. 通常,一个简单的Process.Start(pathToProgram)在这里会有所帮助,但是问题是我正在从安装程序类中调用此行,这意味着msiexec当前正在工作,而我得到的用户名是SYSTEM,而不是当前的实际用户。登录到Windows。

Of course that Environment.UserName also returns "SYSTEM" instead of the logged in user. 当然,该Environment.UserName也会返回“ SYSTEM”,而不是已登录的用户。

How can I start the process as the logged in user and not as the user SYSTEM? 如何以登录用户而不是系统用户身份启动进程?

You can get the userName for current logged in user like this - 您可以像这样获得当前登录用户的用户名-

string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

From SO post - SO后 -

Process p = new Process();
p.StartInfo.FileName = "C:\SetupVM.bat";
p.StartInfo.UserName = userName;
p.StartInfo.Password = "AdminPassword";
p.Start();
p.WaitForExit();

I think that when you call Environment.UserName you get the value for the user impersonated by the current process, while if you call Environment.GetEnvironmentVariable("USERNAME") you get the name of the user that starts the process and at that point the process is not started and, of coure, has not yet impersonated another user. 我认为,当您调用Environment.UserName将获得当前进程模拟的用户的值,而如果您调用Environment.GetEnvironmentVariable("USERNAME") ,则将获得启动该进程的用户的名称,然后进程尚未启动,并且当然还没有模仿其他用户。

However, this is only a raw thought that seems to work for the OP question, but I still searching some reference material to confirm my assertion. 但是,这只是一个原始想法,似乎对OP问题有用,但我仍在搜索一些参考资料以确认我的主张。
I welcome any one that has a better understanding of this fact and explain it better than me 我欢迎任何对这一事实有更好理解的人,并比我更好地解释它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM