C#修改电脑密码,重置电脑密码DirectoryEntry实现
2012-10-15 15:08 苏飞 阅读(2985) 评论(2) 编辑 收藏 举报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(); } }
本人的博客不再维护从2013年就不再维护了
需要我帮助的朋友请到我的个人论坛 http://www.sufeinet.com 进行讨论,感谢大家对我的支持!