I have scoured the internet and there are suggestions that work for certain scenarios, but none work for me. I'm creating a task in a custom action inside the installer. The installer runs as SYSTEM, so instead of my domain\username I get SYSTEM. And, I'm installing a service, which also runs as SYSTEM. Is there a way of getting the actual current user's name, short of asking the user to enter manually? Here's my code:
//Works, I get "Bob-PC\Bob" if I run the code as a user. If it's run as a system
// I get "WORKGROUP\SYSTEM"
string user = Environment.UserDomainName + "\\"+ Environment.UserName;
// doesn't work, returns "NT AUTHORITY\SYSTEM"
string user2 = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
// doesn't work at all, this is not ASP.NET
string user3 = "";//Request.LogonUserIdentity.Name;
// doesn't work, my thread is unnamed
string user4 = System.Threading.Thread.CurrentPrincipal.Identity.Name;
MessageBox.Show("user " + user + "\nuser2 " + user2 + "\nuser3 " + user3 + "\nuser4 " + user4);
tf.RegisterTaskDefinition("MyTask", td,
TaskCreation.CreateOrUpdate,
user,
null,
TaskLogonType.InteractiveToken,
null);