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

在C#使用DirectoryEntry类操作Windows帐户

这几天使用C#操作windows帐户相当纠结,以前没做过,google翻阅了不少资料,尝试不少方法,终于解决了我的问题。

1.创建windows帐户

01 /// <summary>
02 /// 创建Windows帐户
03 /// </summary>
04 /// <param name="pathname"></param>
05 /// <returns></returns>
06 public static void CreateLocalUser(string username, string password, string description)
07 {
08     DirectoryEntry localMachine = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer");
09     var newUser = localMachine.Children.Add(username, "user");
10     newUser.Invoke("SetPassword", new object[] { password });
11     newUser.Invoke("Put", new object[] { "Description", description });
12     newUser.CommitChanges();
13     localMachine.Close();
14     newUser.Close();
15 }

2.更改Windows帐户密码

01 /// <summary>
02 /// 更改Windows帐户密码
03 /// </summary>
04 /// <param name="username"></param>
05 /// <param name="oldPwd"></param>
06 /// <param name="newPwd"></param>
07 public static void ChangeWinUserPasswd(string username, string oldPwd, string newPwd)
08 {
09     DirectoryEntry localMachine =
补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,