代码闲暇时间,将做工程过程中比较常用的代码段备份一次,下边代码段是关于C#创建windows系统用户的代码,希望对各朋友有些用途。
public static bool CreateLocalWindowsAccount(string username, string password, string displayName, string description, bool canChangePwd, bool pwdExpires) { try { PrincipalContext context = new PrincipalContext(ContextType.Machine); UserPrincipal user = new UserPrincipal(context); user.SetPassword(password); user.DisplayName = displayName; user.Name = username; user.Description = description; user.UserCannotChangePassword = canChangePwd; user.PasswordNeverExpires = pwdExpires; user.Save();
GroupPrincipal group = GroupPrincipal.FindByIdentity(context, "Users");
group.Members.Add(user);
group.Save();
return true;
}
catch (Exception ex)
{
MessageBox.Show("Error creating account: {0}", ex.Message);
return false;
}
复制代码
}