Net C # create a Windows User

Source: Internet
Author: User

 

/The premise is that the current user has the corresponding Permissions

/Winnt user management
Using system;
Using system. directoryservices;
Namespace host. adminmanager. inc
{
Public class windwosuser
{
// Create an NT user
// Input parameter: username, userpassword, and path of the main folder
Public static bool createntuser (string username, string userpassword, string path)
{
Directoryentry obdirentry = NULL;
Try
{
Obdirentry = new directoryentry ("winnt: //" + environment. machinename );

Directoryentry obuser = obdirentry. Children. Add (username, "user"); // Add a user name
Obuser. properties ["fullname"]. Add (username); // full name of the user
Obuser. Invoke ("setpassword", userpassword); // User Password
Obuser. Invoke ("put", "Description", "test user from. Net"); // user description
// Obuser. Invoke ("put", "passwordexpired", 1); // you need to change the password upon next logon.
Obuser. Invoke ("put", "userflags", 66049); // The password never expires.
Obuser. Invoke ("put", "homedirectory", PATH); // main folder path
Obuser. commitchanges (); // Save the user
Directoryentry GRP = obdirentry. Children. Find ("users", "group"); // Users Group
If (GRP. Name! = "")
{
GRP. Invoke ("add", obuser. Path. tostring (); // Add a user to a group
}
Return true;
}
Catch
{
Return false;
}
}
// Delete an NT user
// Input parameter: Username
Public static bool delntuser (string username)
{
Try
{
Directoryentry obcomputer = new directoryentry ("winnt: //" + environment. machinename); // obtain the computer instance
Directoryentry obuser = obcomputer. Children. Find (username, "user"); // find the user
Obcomputer. Children. Remove (obuser); // delete a user
Return true;
}
Catch
{
Return false;
}
}

// Modify the NT user password
// Input parameter: username, userpassword, New Password
Public static bool initntpwd (string username, string userpassword)
{
Try
{
Directoryentry obcomputer = new directoryentry ("winnt: //" + environment. machinename );
Directoryentry obuser = obcomputer. Children. Find (username, "user ");
Obuser. Invoke ("setpassword", userpassword );
Obuser. commitchanges ();
Obuser. Close ();
Obcomputer. Close ();
Return true;
}
Catch
{
Return false;
}
}
}
}

 

Http://topic.csdn.net/u/20100902/08/2ecbcb74-158f-4530-b3c6-5a805a4f81b2.html

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.