鉴于此方法功力强大请各位武林好汉不要拿来做坏事哟,下面进入正题。
C#修改电脑密码方法如下:
[code lang=”csharp”] ///
///
/// /// /// public static void ChangeWinUserPasswd(string username, string oldPwd, string newPwd)
{
try
{
DirectoryEntry localMachine = new DirectoryEntry(“WinNT://” + Environment.MachineName + “,computer”);
DirectoryEntry user = localMachine.Children.Find(username, “user”);
object[] password = new object[] { oldPwd, newPwd };
object ret = user.Invoke(“ChangePassword”, password);
user.CommitChanges();
localMachine.Close();
user.Close();
}
catch (Exception)
{
}
}[/code]
C#重置电脑密码如下:
[code lang=”csharp”] ///
///
/// 用户名 /// 新密码 public static void ResetUserPassword(string userName, string password)
{
string _Path = “WinNT://” + Environment.MachineName;
DirectoryEntry machine = new DirectoryEntry(_Path); //获得计算机实例
DirectoryEntry user = machine.Children.Find(userName, “User”); //找得用户
if (user != null)
{
user.Invoke(“SetPassword”, password); //用户密码
user.CommitChanges();
}
}[/code]
下面提供一下类大家可以参考一下
[code lang=”csharp”]//1.创建windows帐户
///
///
/// ///
public static void CreateLocalUser(string username, string password, string description)
{
DirectoryEntry localMachine = new DirectoryEntry(“WinNT://” + Environment.MachineName + “,computer”);
var newUser = localMachine.Children.Add(username, “user”);
newUser.Invoke(“SetPassword”, new object[] { password });
newUser.Invoke(“Put”, new object[] { “Description”, description });
newUser.CommitChanges();
localMachine.Close();
newUser.Close();
}
//2.更改Windows帐户密码
///
///
/// /// /// public static void ChangeWinUserPasswd(string username, string oldPwd, string newPwd)
{
DirectoryEntry localMachine = new DirectoryEntry(“WinNT://” + Environment.MachineName + “,computer”);
DirectoryEntry user = localMachine.Children.Find(username, “user”);
object[] password = new object[] { oldPwd, newPwd };
object ret = user.Invoke(“ChangePassword”, password);
user.CommitChanges();
localMachine.Close();
user.Close();
}
//3.判断Windows用户是否存在
///
///
/// ///
public static bool ExistWinUser(string username)
{
try
{
using (DirectoryEntry localMachine = new DirectoryEntry(“WinNT://” + Environment.MachineName + “,computer”))
{
var user = localMachine.Children.Find(username, “user”);
return user != null;
}
}
catch
{
return false;
}
}
//4.删除Windows用户
///
///
/// ///
public static bool DeleteWinUser(string username)
{
try
{
using (DirectoryEntry localMachine = new DirectoryEntry(“WinNT://” + Environment.MachineName + “,computer”))
{
//删除存在用户
var delUser = localMachine.Children.Find(username, “user”);
if (delUser != null)
{
localMachine.Children.Remove(delUser);
}
}
return true;
}
catch
{
return false;
}
}
//5.启用/禁用windows帐户
///
///
/// public static void Disable(string username, bool isDisable)
{
var userDn = “WinNT://” + Environment.MachineName + “/” + username + “,user”;
DirectoryEntry user = new DirectoryEntry(userDn);
user.InvokeSet(“AccountDisabled”, isDisable);
user.CommitChanges();
user.Close();
}[/code]
操作windows帐户的方法的诀窍在于通过DirectoryEntry 实例调用Invoke,InvokeGet,InvokeSet这三个方法。此三个方法可以对对本机 Active Directory 对象调用方法。操作win帐户的Active Directory 对象就是IADsUser接口。DirectoryEntry 实例通过调用Invoke方法调用IADsUser接口的方法,如上面修改Windows帐户密码就是通过调用IADsUser接口的“ChangePassword”方法;通过InvokeGet和InvokeSet方法调用IADsUser接口的属性,如上面的启用/禁用windows帐户,调用IADsUser接口的“AccountDisabled”属性
本博客所有文章如无特别注明均为原创
如果觉得对你有帮助,可以通过下方打赏对作者表示鼓励
本文采用知识共享署名-非商业性使用-相同方式共享
如若转载,请注明出处:《C#修改电脑密码,重置电脑密码DirectoryEntry实现》https://www.fangsi.net/249.html