C#创建windows系统用户的方法
程序员文章站
2023-12-12 14:52:22
本文实例讲述了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#程序设计有所帮助。