C#創(chuàng)建windows系統(tǒng)用戶
2018-07-20 來源:open-open

下面的代碼可以通過c#創(chuàng)建一個windows的本地系統(tǒng)賬戶,參數(shù)包括用戶名、密碼、顯示名稱、描述、是否強(qiáng)制修改密碼、密碼是否過期
/// <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; } }
版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。
最新資訊
熱門推薦