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(); } }