当前位置:软件学习 > Director >>

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)
            {
              
            }
        } www.zzzyk.com
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# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,