0

Im developing a winforms wizard and in one page of this wizard i need to list all the accounts to enable the user select one. So my working code is:

  private static IEnumerable<string> GetUsersAccountName()
    {
        var searcher = new ManagementObjectSearcher(new SelectQuery("Win32_UserAccount"));
        var usersAccounts = (from ManagementBaseObject envVar in searcher.Get()
                     select envVar["Name"].ToString());

        return users.ToList();
    }

So with this method i get the name of the accounts, but not get the "local system account", how to get this account name too?

gog
  • 11,788
  • 23
  • 67
  • 129
  • LocalSystem is not part of that mechanic; it's ignored by the security system. If you really need it, just add it manually – Alex K. Oct 03 '14 at 15:04
  • http://stackoverflow.com/questions/1240373/how-do-i-get-the-current-username-in-net-using-c is this what you are looking for ? – Gmnd-i Oct 03 '14 at 15:35

1 Answers1

1

You're right, there's a little stuff you need to do, in order to receive the account name. You can do look up by SID. In fact, SID is always the same for LocalSystem(S-1-5-18).

To actually convert it, try this:

string localSysAccountName = new SecurityIdentifier("S-1-5-18")
                     .Translate(typeof(NTAccount)).ToString();

In case that does not seem to work well, have a look at LookupAccountSid.

Community
  • 1
  • 1
Erti-Chris Eelmaa
  • 25,338
  • 6
  • 61
  • 78