Using system;
Using system. Collections. Generic;
Using system. text;
Using system. directoryservices;
Namespace systemuseroperationdll
{
Public class systemuseroperationclass
{
Private directoryentry ad;
/// <Summary>
/// Initialization Method
/// </Summary>
/// <Param name = "strmachinename"> machine name </param>
/// <Param name = "strloginname"> logon username (for example, Administrator) </param>
/// <Param name = "strloginpassword"> logon password </param>
Public systemuseroperationclass (string strmachinename, string strloginname, string strloginpassword)
{
AD = new directoryentry ("winnt: //" + strmachinename + ", computer", ". \" + strloginname, strloginpassword );
}
/// <Summary>
/// Add a system (domain) User
/// </Summary>
/// <Param name = "strusername"> User Name </param>
/// <Param name = "strpassword"> password </param>
/// <Param name = "strdescription"> description </param>
/// <Returns> </returns>
Public bool adddomainuser (string strusername, string strpassword, string strdescription)
{
Try
{
Directoryentry newuser;
Newuser = AD. Children. Add (strusername, "user"); // Add a user
Newuser. Invoke ("setpassword", new object [] {strpassword}); // set the password
Newuser. properties ["Description"]. Add (strdescription); // Add description
Newuser. commitchanges ();
Ad. Close ();
}
Catch
{
Throw;
}
Return true;
}
/// <Summary>
/// Modify the system (domain) User
/// </Summary>
/// <Param name = "strusername"> User Name </param>
/// <Param name = "strpassword"> password </param>
/// <Param name = "strdescription"> description </param>
/// <Returns> </returns>
Public bool modifydomainuser (string strusername, string strpassword, string strdescription)
{
Try
{
Directoryentry user = AD. Children. Find (strusername );
If (user. Name! = NULL)
{
// Change the password
User. Invoke ("setpassword", new object [] {strpassword });
// Modify the description
User. properties ["Description"]. value = strdescription;
User. commitchanges ();
}
Ad. Close ();
}
Catch
{
Throw;
}
Return true;
}
/// <Summary>
/// Delete a system (domain) User
/// </Summary>
/// <Param name = "strusername"> User Name </param>
/// <Returns> </returns>
Public bool deletedomainuser (string strusername)
{
Try
{
Directoryentry user = AD. Children. Find (strusername, "user"); // locate the user to delete
If (user. Name! = NULL)
{
Ad. Children. Remove (User );
}
Ad. Close ();
}
Catch
{
Throw;
}
Return true;
}
/// <Summary>
/// Add a system (domain) Group
/// </Summary>
/// <Param name = "strgroupname"> group name </param>
/// <Param name = "strdescription"> description </param>
/// <Returns> </returns>
Public bool adddomaingroup (string strgroupname, string strdescription)
{
Try
{
Directoryentry group;
Group = AD. Children. Add (strgroupname, "group ");
Group. properties ["Description"]. Add (strdescription );
Group. commitchanges ();
Ad. Close ();
}
Catch
{
Throw;
}
Return true;
}
/// <Summary>
/// Modify the system (domain) Group
/// </Summary>
/// <Param name = "strgroupname"> group name </param>
/// <Param name = "strdescription"> description </param>
/// <Returns> </returns>
Public bool modifydomaingroup (string strgroupname, string strdescription)
{
Try
{
Directoryentry group = AD. Children. Find (strgroupname );
If (group. Name! = NULL)
{
Group. properties ["Description"]. value = strdescription;
Group. commitchanges ();
}
Ad. Close ();
}
Catch
{
Throw;
}
Return true;
}
/// <Summary>
/// Delete the system (domain) Group
/// </Summary>
/// <Param name = "strgroupname"> group name </param>
/// <Returns> </returns>
Public bool deletedomaingroup (string strgroupname)
{
Try
{
Directoryentry group = AD. Children. Find (strgroupname, "group ");
If (group. Name! = NULL)
{
Ad. Children. Remove (group );
}
Ad. Close ();
}
Catch
{
Throw;
}
Return true;
}
/// <Summary>
/// Add group users
/// </Summary>
/// <Param name = "strgroupname"> group name </param>
/// <Param name = "strusername"> User Name </param>
/// <Returns> </returns>
Public bool addgroupuser (string strgroupname, string strusername)
{
Try
{
Directoryentry group = AD. Children. Find (strgroupname, "group"); // locate the group
Directoryentry user = AD. Children. Find (strusername, "user"); // find the user
If (group. Name! = NULL & User. Name! = NULL)
{
Group. Invoke ("add", new object [] {user. Path });
}
Ad. Close ();
}
Catch
{
Throw;
}
Return true;
}
/// <Summary>
/// Remove a group of users
/// </Summary>
/// <Param name = "strgroupname"> group name </param>
/// <Param name = "strusername"> User Name </param>
/// <Returns> </returns>
Public bool removegroupuser (string strgroupname, string strusername)
{
Try
{
Directoryentry group = AD. Children. Find (strgroupname, "group"); // locate the group
Object members = group. Invoke ("members", null );
Foreach (Object member in (system. Collections. ienumerable) members)
{
// Obtain each member of the group
Directoryentry x = new directoryentry (member );
If (strusername = x. Name) // if the user to be removed exists, the user is removed from the group.
{
Directoryentry user = AD. Children. Find (strusername, "user"); // locate the user
Group. Invoke ("Remove", new object [] {user. Path });
}
}
Ad. Close ();
}
Catch
{
Throw;
}
Return true;
}
}
}
Please kindly advise if you have any shortcomings in this regard for the first time.