本文实例讲述了C#创建windows系统用户的方法。分享给大家供大家参考。具体如下:
下面的代码可以通过c#创建一个windows的本地系统账户,参数包括用户名、密码、显示名称、描述、是否强制修改密码、密码是否过期
///<summary> ///methodtocreateanewlocalWindowsuseraccount ///</summary> ///<paramname="username">Usernameofthenewaccount</param> ///<paramname="password">Passwordofthenewaccount</param> ///<paramname="displayName">Accountdisplayname</param> ///<paramname="description">Accountdescription</param> ///<paramname="canChangePwd">Valueofwhetherthenewusercanchangetheirpassword</param> ///<paramname="pwdExpires">Valuedeterminingifthepasswordeverexpires</param> publicstaticboolCreateLocalWindowsAccount(stringusername,stringpassword,stringdisplayName,stringdescription,boolcanChangePwd,boolpwdExpires) { try { PrincipalContextcontext=newPrincipalContext(ContextType.Machine); UserPrincipaluser=newUserPrincipal(context); user.SetPassword(password); user.DisplayName=displayName; user.Name=username; user.Description=description; user.UserCannotChangePassword=canChangePwd; user.PasswordNeverExpires=pwdExpires; user.Save(); //nowadduserto"Users"groupsoitdisplaysinControlPanel GroupPrincipalgroup=GroupPrincipal.FindByIdentity(context,"Users"); group.Members.Add(user); group.Save(); returntrue; } catch(Exceptionex) { MessageBox.Show("Errorcreatingaccount:{0}",ex.Message); returnfalse; } }
希望本文所述对大家的C#程序设计有所帮助。
您可能感兴趣的文章:c#通过进程调用cmd判断登录用户权限代码分享C#实现将选中复选框的信息返回给用户的方法c#之用户定义的数据类型转换介绍C#用户定义类型转换详解c#对windows用户和组操作实例C#Winform禁止用户调整ListView的列宽asp.net(c#)限制用户输入规定的字符和数字的代码C#获取系统进程的用户名C#实现判断当前操作用户管理角色的方法
利用C语言的Cairo图形库绘制太极图实例教程
stringstream操纵string的方法上述就是C#学习教程:C#创建windows系统用户的方法分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请点击右边联系管理员删除。
如若转载,请注明出处:https://www.ctvol.com/cdevelopment/906149.html