Introduction
In our previous article, How to get
full name of logged in user, we showed how you can get full name of a user
in a given domain or machine. You can extend that idea to obtain any information
you want for a given user. This artcile will describe how you can add a new user
account into your domain or machine using .Net DirectoryServices classes. We
will be using WinNT provider for illustrations in this article. But you
can extend the examples to use LDAP or AD providers.
Details
Here are the key steps that you will need to perform to create new account.
The following code demonstrates all the steps that we described above.
private void AddUser(string strDoamin, string strLogin, string strPwd)
{
DirectoryEntry obDirEntry = null;
try
{
obDirEntry = new DirectoryEntry("WinNT://" + strDoamin);
DirectoryEntries entries = obDirEntry.Children;
DirectoryEntry obUser = entries.Add(strLogin, "User");
obUser.Properties["FullName"].Add("Amigo");
object obRet = obUser.Invoke("SetPassword", strPwd);
obUser.CommitChanges();
}
catch (Exception ex)
{
Trace.Warn(ex.Message);
}
}
Please feel free to send your suggestions directly to us at softomatix@pardesiservices.com.