C#創立windows體系用戶的辦法。本站提示廣大學習愛好者:(C#創立windows體系用戶的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#創立windows體系用戶的辦法正文
本文實例講述了C#創立windows體系用戶的辦法。分享給年夜家供年夜家參考。詳細以下:
上面的代碼可以經由過程c#創立一個windows的當地體系賬戶,參數包含用戶名、暗碼、顯示稱號、描寫、能否強迫修正暗碼、暗碼能否過時
/// <summary> /// method to create a new local Windows user account /// </summary> /// <param name="username">Username of the new account</param> /// <param name="password">Password of the new account</param> /// <param name="displayName">Account display name</param> /// <param name="description">Account description</param> /// <param name="canChangePwd">Value of whether the new user can change their password</param> /// <param name="pwdExpires">Value determining if the password ever expires</param> 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(); //now add user to "Users" group so it displays in Control Panel 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; } }
願望本文所述對年夜家的C#法式設計有所贊助。