SamKr
12/10/2019 - 10:52 AM

Search Local User

Search for the existance of a local user

using System.DirectoryServices.AccountManagement;

using (var pc = new PrincipalContext(ContextType.Machine))
{
    var up = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, "gebruiker2");
    var userExists = (up != null);

    if (userExists) Console.WriteLine("user exists");
    else Console.WriteLine("user not found");
}