C # add, delete, and modify windows users and groups
Source: Internet
Author: User
For more information about the invoke call parameters of directoryentry, see: http://msdn.microsoft.com/zh-cn/library/aa746340 (En-US ). aspxusing system; using system. collections. generic; using system. LINQ; using system. text; using system. directoryservices; namespace usage. framework. security {// <summary> /// computer user and group operation class // </Summary> public class userandgrouphelper {Private Static readonly string Path = "winnt: // "+ environment. machinename; // <sum Mary> /// Add a Windows user /// </Summary> /// <Param name = "username"> User Name </param> /// <Param name = "Password "> password </param> /// <Param name =" group "> group </param> /// <Param name =" Description "> description </param> public static void adduser (string username, string password, string group, string description) {using (directoryentry dir = new directoryentry (PATH) {using (directoryentry user = dir. children. add (username, "User") // Add the user name {user. properties ["fullname"]. add (username); // The full name of the user. invoke ("setpassword", password); // user password. invoke ("put", "Description", description); // detailed user description // user. invoke ("put", "passwordexpired", 1); // you need to change the password user for the next login. invoke ("put", "userflags", 66049); // The Password Never Expires // user. invoke ("put", "userflags", 0x0040); // the user cannot change the password s user. commitchanges (); // save user using (directoryentry GRP = dir. children. fi Nd (group, "group") {If (GRP. Name! = "") {GRP. invoke ("add", user. path. tostring ()); // Add a user to a group }}}/// <summary> /// change the Windows User Password /// </Summary> /// <Param name = "username"> User Name </param> // <Param name = "newpassword"> New Password </param> Public static void updateuserpassword (string username, string newpassword) {using (directoryentry dir = new directoryentry (PATH) {using (directoryentry user = dir. children. find (username, "user") {user. invoke ("setpassword", new object [] {newpassword}); User. commitchanges ();}}} /// <summary> /// delete a Windows user /// </Summary> /// <Param name = "username"> username </param> Public static void removeuser (string username) {using (directoryentry dir = new directoryentry (PATH) {using (directoryentry user = dir. children. find (username, "user") {dir. children. remove (User );}}} /// <summary> /// Add a Windows User Group /// </Summary> /// <Param name = "groupname"> group name </param> // <param name = "Description"> description </param> Public static void addgroup (string groupname, string description) {using (directoryentry dir = new directoryentry (PATH) {using (directoryentry group = dir. children. add (groupname, "group") {group. invoke ("put", new object [] {"Description", description}); group. commitchanges ();}}} /// <summary> /// Delete the Windows User Group /// </Summary> /// <Param name = "groupname"> group name </param> Public static void removegroup (string groupname) {using (directoryentry dir = new directoryentry (PATH) {using (directoryentry group = dir. children. find (groupname, "group") {dir. children. remove (Group );}}}}}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service