本文实例讲述了C#修改及重置电脑密码DirectoryEntry实现方法。分享给大家供大家参考。具体如下:
C#修改电脑密码方法如下:
/// <summary>/// 更改Windows帐户密码/// </summary>/// <param name="username"></param>/// <param name="oldPwd"></param>/// <param name="newPwd"></param>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) { }}
C#重置电脑密码:
/// <summary>/// 重置指定用户的密码/// </summary>/// <param name="userName">用户名</param>/// <param name="password">新密码</param>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(); }}
希望本文所述对大家的C#程序设计有所帮助。
新闻热点
疑难解答