C#

推荐列表 站点导航

当前位置:首页 > 脚本编程 > C# >

C#_C#创建windows系统用户的方法,本文实例讲述了C#创建windows系

来源:网络整理  作者:  发布时间:2020-12-22 13:21
C#创建windows系统用户的方法,本文实例讲述了C#创建windows系统用户的方法。分享给大家供大家参考。具体如下:下面的...

下面的代码可以通过c#创建一个windows的本地系统账户,参数包括用户名、密码、显示名称、描述、是否强制修改密码、密码是否过期

/// <summary> /// method to create a new local Windows user account /// </summary> /// <param>Username of the new account</param> /// <param>Password of the new account</param> /// <param>Account display name</param> /// <param>Account description</param> /// <param>Value of whether the new user can change their password</param> /// <param>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; } }

相关热词: windows 方法 C# 实例

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!

本文地址: https://v30.fanwenzhu.com/jiaob/c/7133.shtml

最新文章
热门文章
Copyright © www.juheyunku.com      关于 | 合作 | 声明 | 联系 | 更新 | 地图 | Tags

C#_C#创建windows系统用户的方法,本文实例讲述了C#创建windows系

2020-12-22 编辑:

下面的代码可以通过c#创建一个windows的本地系统账户,参数包括用户名、密码、显示名称、描述、是否强制修改密码、密码是否过期

/// <summary> /// method to create a new local Windows user account /// </summary> /// <param>Username of the new account</param> /// <param>Password of the new account</param> /// <param>Account display name</param> /// <param>Account description</param> /// <param>Value of whether the new user can change their password</param> /// <param>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; } }

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供学习参考!
本文地址为 https://v30.fanwenzhu.com/jiaob/c/7133.shtml

相关文章

风云图片

推荐阅读

返回C#频道首页