using System;
using System.DirectoryServices;
namespace test
{
public partial class CreateNewAccount: Form
{
public CreateNewAccount()
{
InitializeComponent();
}
private void btnCreate_Click(object sender, EventArgs e)
{
//创建新用户信息
try
{
DirectoryEntry AD = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer");//类DirectoryEntry封装Active Directory层次结构中的节点或对象
DirectoryEntry NewUser = AD.Children.Add("TestName", "User");//帐号
NewUser.Invoke("SetPassWord", new object[] { "111" });//密码
NewUser.Invoke("Put", new object[] { "Description", "Test User from .NET" });
NewUser.CommitChanges();
DirectoryEntry grp;
grp = AD.Children.Find("Guests", "group");//Find()返回此集合中具有指定名称和指定类型的成员
if (grp != null) { grp.Invoke("Add", new object[] { NewUser.Path.ToString() }); }
MessageBox.Show("Account Created Successfully", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
using System.DirectoryServices;
namespace test
{
public partial class CreateNewAccount: Form
{
public CreateNewAccount()
{
InitializeComponent();
}
private void btnCreate_Click(object sender, EventArgs e)
{
//创建新用户信息
try
{
DirectoryEntry AD = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer");//类DirectoryEntry封装Active Directory层次结构中的节点或对象
DirectoryEntry NewUser = AD.Children.Add("TestName", "User");//帐号
NewUser.Invoke("SetPassWord", new object[] { "111" });//密码
NewUser.Invoke("Put", new object[] { "Description", "Test User from .NET" });
NewUser.CommitChanges();
DirectoryEntry grp;
grp = AD.Children.Find("Guests", "group");//Find()返回此集合中具有指定名称和指定类型的成员
if (grp != null) { grp.Invoke("Add", new object[] { NewUser.Path.ToString() }); }
MessageBox.Show("Account Created Successfully", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
类DirectoryEntry封装Active Directory层次结构中的节点或对象