try (by Billy Zhang from Microsoft at
http://groups.google.com/groups?q=C%23+ADSI+Delete+User+DirectoryEntry&hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=AQlrwzxTCHA.3184%40cpmsftngxa10&rnum=3
)using System.DirectoryServices;
using System;
class Tester
{
// User flags used to set user properties see Platform SDK and ADSI doc's in MSDN const int UF_SCRIPT = 0x0001;
const int UF_ACCOUNTDISABLE = 0x0002;
const int UF_HOMEDIR_REQUIRED = 0x0008;
const int UF_LOCKOUT = 0x0010;
const int UF_PASSWD_NOTREQD = 0x0020;
const int UF_PASSWD_CANT_CHANGE = 0x0040;
const int UF_TEMP_DUPLICATE_ACCOUNT = 0x0100;
const int UF_NORMAL_ACCOUNT = 0x0200;
const int UF_DONT_EXPIRE_PASSWD = 0x10000;
const int UF_PASSWORD_EXPIRED = 0x800000;
public static void Main()
{
try
{
string strNodeName = "SomeUser";
DirectoryEntry NewUser;
//Bind and get the computer container
DirectoryEntry AD = new DirectoryEntry("WinNT://remMachineName,computer", ".\\administrator", "adminpwd"); // specify explicit credentials used to bind
// delete user when existing
try
{
// this throws when no such user
NewUser = AD.Children.Find(strNodeName, "User");
AD.Children.Remove(NewUser);
}
// Catch not found exception
catch(COMException cex)
{
Console.WriteLine(cex.Message);
}
// Add user using the user schema
NewUser = AD.Children.Add(strNodeName, "user");
NewUser.Properties["description"].Add("test user");
NewUser.Properties["PasswordExpired"].Add(1); // user must change password at next login // Set user flags sample here sets Normal Account
NewUser.Properties["userFlags"].Add(UF_NORMAL_ACCOUNT);
// invoke native method 'SetPassword' before CommitChanges
NewUser.Invoke("SetPassword", new Object[] {"#12345Abc"});
NewUser.CommitChanges();
// Add user to guests alias
DirectoryEntry grp = AD.Children.Find("Guests", "group");
if (grp.Name != null)
grp.Invoke("Add", new Object[] {NewUser.Path.ToString()});
Console.WriteLine("Account Created Successfully");
AD.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
http://groups.google.com/groups?q=C%23+ADSI+Delete+User+DirectoryEntry&hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=AQlrwzxTCHA.3184%40cpmsftngxa10&rnum=3
)using System.DirectoryServices;
using System;
class Tester
{
// User flags used to set user properties see Platform SDK and ADSI doc's in MSDN const int UF_SCRIPT = 0x0001;
const int UF_ACCOUNTDISABLE = 0x0002;
const int UF_HOMEDIR_REQUIRED = 0x0008;
const int UF_LOCKOUT = 0x0010;
const int UF_PASSWD_NOTREQD = 0x0020;
const int UF_PASSWD_CANT_CHANGE = 0x0040;
const int UF_TEMP_DUPLICATE_ACCOUNT = 0x0100;
const int UF_NORMAL_ACCOUNT = 0x0200;
const int UF_DONT_EXPIRE_PASSWD = 0x10000;
const int UF_PASSWORD_EXPIRED = 0x800000;
public static void Main()
{
try
{
string strNodeName = "SomeUser";
DirectoryEntry NewUser;
//Bind and get the computer container
DirectoryEntry AD = new DirectoryEntry("WinNT://remMachineName,computer", ".\\administrator", "adminpwd"); // specify explicit credentials used to bind
// delete user when existing
try
{
// this throws when no such user
NewUser = AD.Children.Find(strNodeName, "User");
AD.Children.Remove(NewUser);
}
// Catch not found exception
catch(COMException cex)
{
Console.WriteLine(cex.Message);
}
// Add user using the user schema
NewUser = AD.Children.Add(strNodeName, "user");
NewUser.Properties["description"].Add("test user");
NewUser.Properties["PasswordExpired"].Add(1); // user must change password at next login // Set user flags sample here sets Normal Account
NewUser.Properties["userFlags"].Add(UF_NORMAL_ACCOUNT);
// invoke native method 'SetPassword' before CommitChanges
NewUser.Invoke("SetPassword", new Object[] {"#12345Abc"});
NewUser.CommitChanges();
// Add user to guests alias
DirectoryEntry grp = AD.Children.Find("Guests", "group");
if (grp.Name != null)
grp.Invoke("Add", new Object[] {NewUser.Path.ToString()});
Console.WriteLine("Account Created Successfully");
AD.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货