C#如何直接更改用户的电脑密码?
C#代码示例:
string username = "我是用户"; // 要更改密码的用户名
string newPassword = "我是密码"; // 新密码
try
{
using (PrincipalContext context = new PrincipalContext(ContextType.Machine))
{
UserPrincipal user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, username);
if (user != null)
{
user.SetPassword(newPassword);
user.Save();
//成功后的逻辑
}
else
{
//添加失败的逻辑
}
}
}
catch
{
//添加异常的逻辑
}