public bool CreateWindowAccount(string user, string pass, string group, string Desc)
{
try
{
DirectoryEntry AD = new DirectoryEntry("WinNT://" +
Environment.MachineName + ",computer");
DirectoryEntry NewUser = AD.Children.Add(user, "user"); //帐号
NewUser.Invoke("SetPassword", new object[] { pass }); // 密码
NewUser.Invoke("Put", new object[] { "Description", Desc });
NewUser.CommitChanges();
DirectoryEntry grp; grp = AD.Children.Find(group, "group");
if (grp != null) { grp.Invoke("Add", new object[] { NewUser.Path.ToString() }); }
return true;
}
catch
{
//System.Web.HttpContext.Current.Response.Write("增加Win系统用户失败<br />错误提示:" + er.Message);
return false;
}
}
在XP和2000下测试成功,但在2003下不能写入,请问该怎么改呀~谢谢~
using System.Collections.Generic;
using System.Text;
using System.DirectoryServices;
namespace ChangePwdWebpart
{
class OperateDirectory
{
//修改密码public static bool ChangePwd(string UserName, string oldPwd, string newPwd)
{
try
{
DirectoryEntry MachineDirectoryEntry;
MachineDirectoryEntry = new DirectoryEntry("WinNT://" + System.Environment.MachineName);
DirectoryEntry CurrentDirectoryEntry = MachineDirectoryEntry.Children.Find(UserName);
CurrentDirectoryEntry.Invoke("ChangePassword", new Object[] { oldPwd, newPwd });
CurrentDirectoryEntry.CommitChanges();
CurrentDirectoryEntry.Close();
return true;
}
catch (Exception exp)
{
if (exp.InnerException.Message.Replace("'", "").IndexOf("网络密码不正确") != -1)
throw new Exception("密码修改失败,输入的原始密码不正确");
else
throw new Exception(exp.InnerException.Message);
}
finally
{
}
}
//新增组:
private void AddGroup()
{
string path = String.Format("WinNT://{0}", System.Environment.MachineName);
DirectoryEntry entryPC = new DirectoryEntry(path);
DirectoryEntry newEntry = entryPC.Children.Add("NewGroup", "Group");
//newEntry.Properties["groupType"][0] = "4";
newEntry.Properties["Description"].Add("test");
newEntry.CommitChanges();
}
//删除组:
private void DelGroup()
{
string userGroup = "NewGroup";
string path1 = String.Format("WinNT://{0}", System.Environment.MachineName);
DirectoryEntry parent = new DirectoryEntry(path1);
object[] paras = new object[2];
paras[0] = "group";
paras[1] = userGroup;
parent.Invoke("Delete", paras);
}
//查找组:
private void FindGroup()
{
string userGroup = "NewGroup";
string path1 = String.Format("WinNT://{0}", System.Environment.MachineName);
DirectoryEntry parent = new DirectoryEntry(path1);
DirectoryEntry group = parent.Children.Find(userGroup, "group");
if (group != null)
MessageBox.Show("Group find.");
else
MessageBox.Show("Group not found.");
}
//新增用户:private void AddUser()
{
string path = String.Format("WinNT://{0}", System.Environment.MachineName);
DirectoryEntry entryPC = new DirectoryEntry(path);
DirectoryEntry obUser = entryPC.Children.Add("NewUser", "User");
obUser.Properties["Description"].Add("Test User from .NET");
obUser.Properties["FullName"].Add("NewUser");
object obRet = obUser.Invoke("SetPassword", "123");
obUser.CommitChanges();
}
//删除用户: private void Deluser()
{
string userName = "NewUser";
string path1 = String.Format("WinNT://{0}", System.Environment.MachineName);
DirectoryEntry parent = new DirectoryEntry(path1);
object[] paras = new object[2];
paras[0] = "user";
paras[1] = userName;
parent.Invoke("Delete", paras);
}
//查找用户: private void findUser()
{
string userName = "NewUser";
string path1 = String.Format("WinNT://{0}", System.Environment.MachineName);
DirectoryEntry parent = new DirectoryEntry(path1);
DirectoryEntry user = parent.Children.Find(userName, "user");
if (user != null)
MessageBox.Show("User find.");
else
MessageBox.Show("User not found.");
}
}
} 全部在这,自己试试看