0

I am trying to get "current active user account name" (Windows user).

For that I have tried the below mentioned code and I get the "current active user account name", but this code first get all users list and then filters (get) the "current active user name".

Now, I am trying to get "current active user account name" (Windows user) but without a for loop and I tried to get help (research) in Microsoft Developer Documentation but I could not able to find helpful resources.

SelectQuery userListQuery = new SelectQuery("Win32_UserAccount");
ManagementObjectSearcher userSearcher = new ManagementObjectSearcher(userListQuery);

foreach (ManagementObject envVar in userSearcher.Get())
{
    // get sid string from username (for to acsess user's current_user registry key)
    NTAccount userAccount = new NTAccount(envVar["Name"].ToString());
    SecurityIdentifier sid = (SecurityIdentifier)userAccount.Translate(typeof(SecurityIdentifier));
    string sidString = sid.ToString();

    File.AppendAllText("c:\\outs\\log.txt", " ---- user account  ----" + userAccount + "\n\n");
    File.AppendAllText("c:\\outs\\log.txt", " ---- sid string  ----" + sidString + "\n\n");
}
  • 2
    Does this answer your question? [How do I get the current username in .NET using C#?](https://stackoverflow.com/questions/1240373/how-do-i-get-the-current-username-in-net-using-c) and [Get Windows User Display Name](https://stackoverflow.com/questions/22187761/get-windows-user-display-name) and [How do I get the currently-logged username from a Windows service in .NET?](https://stackoverflow.com/questions/5218778/how-do-i-get-the-currently-logged-username-from-a-windows-service-in-net) –  Jun 02 '21 at 11:28
  • those answers gives me username but I want to get "current user account name" – umesh_sarkar Jun 02 '21 at 11:44
  • 1
    @.SARKAR Isn't that thing there? https://learn.microsoft.com/dotnet/api/system.security.principal.windowsidentity –  Jun 02 '21 at 11:46
  • From what context are you looking? Is the code you are executing running with a user's token (for example, did a user double click an icon or type the command name in a CMD window)? – Flydog57 Jun 02 '21 at 12:30
  • @Flydog57 this code is in script(with user token). my aim is to get "sid" string and edit registry on specific actions by user in the windows application. – umesh_sarkar Jun 02 '21 at 12:49
  • Don't know what "in script" means, but why wouldn't you use `var user = System.Security.Principal.WindowsIdentity.GetCurrent();` and then look at the details of that user (particularly; `var sid = user.User?.Value;`) – Flydog57 Jun 02 '21 at 13:58
  • And, are you using this to look the current user up by SID in HKEY_ALL_USERS? Why not just use HKEY_CURRENT_USER – Flydog57 Jun 02 '21 at 14:04
  • I need the current windows user account name(system) but `var user = System.Security.Principal.WindowsIdentity.GetCurrent();` returns current active username. – umesh_sarkar Jun 03 '21 at 10:46

1 Answers1

1

I've solved my problem using:

var searcher = new ManagementObjectSearcher("SELECT UserName FROM Win32_ComputerSystem");
var collection = searcher.Get();
string username = (string)collection.Cast<ManagementBaseObject>().First()["UserName"];