[C#] C#创建windows系统用户 →→→→→进入此内容的聊天室

来自 , 2019-07-09, 写在 C#, 查看 127 次.
URL http://www.code666.cn/view/4dcfbc05
  1. /// <summary>
  2. /// method to create a new local Windows user account
  3. /// </summary>
  4. /// <param name="username">Username of the new account</param>
  5. /// <param name="password">Password of the new account</param>
  6. /// <param name="displayName">Account display name</param>
  7. /// <param name="description">Account description</param>
  8. /// <param name="canChangePwd">Value of whether the new user can change their password</param>
  9. /// <param name="pwdExpires">Value determining if the password ever expires</param>
  10. public static bool CreateLocalWindowsAccount(string username, string password, string displayName, string description, bool canChangePwd, bool pwdExpires)
  11. {
  12.     try
  13.     {
  14.         PrincipalContext context = new PrincipalContext(ContextType.Machine);
  15.         UserPrincipal user = new UserPrincipal(context);
  16.         user.SetPassword(password);
  17.         user.DisplayName = displayName;
  18.         user.Name = username;
  19.         user.Description = description;
  20.         user.UserCannotChangePassword = canChangePwd;
  21.         user.PasswordNeverExpires = pwdExpires;
  22.         user.Save();
  23.  
  24.         //now add user to "Users" group so it displays in Control Panel
  25.         GroupPrincipal group = GroupPrincipal.FindByIdentity(context, "Users");
  26.         group.Members.Add(user);
  27.         group.Save();
  28.  
  29.         return true;
  30.     }
  31.     catch (Exception ex)
  32.     {
  33.         MessageBox.Show("Error creating account: {0}", ex.Message);
  34.         return false;
  35.     }
  36.    
  37. }
  38. //csharp/7928

回复 "C#创建windows系统用户"

这儿你可以回复上面这条便签

captcha