Вопрос

I'm using C# with .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.

Of course that Environment.UserName also returns "SYSTEM" instead of the logged in user.

How can I start the process as the logged in user and not as the user SYSTEM?

Это было полезно?

Решение

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.

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.
I welcome any one that has a better understanding of this fact and explain it better than me

Другие советы

You can get the userName for current logged in user like this -

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

From SO post -

Process p = new Process();
p.StartInfo.FileName = "C:\SetupVM.bat";
p.StartInfo.UserName = userName;
p.StartInfo.Password = "AdminPassword";
p.Start();
p.WaitForExit();
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top