今天學習了下怎么用.Net操作本地用戶和組,因為目前網上還沒看到一篇比較完整的文章,所以整理了下也分享出來,最后附帶參考文檔,方便深究的童鞋繼續學習。
========== 原創作品 作者:Yokeqi 出處:博客園 ==========
這里兩個思路,一個是利用WindowsApi進行操作,另一個則是用.net封裝好的DirectoryEntry類。
這里只為快速實現,不求技術高深,所以采用DirectoryEntry是最好的,也容易理解和上手。最后附帶相關的文章鏈接,方面要深究的童鞋慢慢研究。
一、知識點簡單介紹
1. 初始化DirectoryEntry類,傳入域節點,以本機為例:string PATH_LOCAL_MACHINE = "WinNT://" + Environment.MachineName;
DirectoryEntry dir = new DirectoryEntry(PATH_LOCAL_MACHINE)
2. DirectoryEntry類擁有Children屬性,可以通過Children屬性獲取本機所有用戶、組、和服務等對象,並且這里類似於樹形結構,父子節點的類型都是DirectoryEntry,理解這一點后面用起來就簡單很多。
using (DirectoryEntry dir = new DirectoryEntry(_machinePath)) { foreach (DirectoryEntry chd in dir.Children) { } }
3. 通過向DirectoryEntry類的Children屬性Add或Remove對象,實現添加和刪除功能。
using (DirectoryEntry dir = new DirectoryEntry(_machinePath)) { //增加用戶 using (DirectoryEntry user = dir.Children.Add(name, "User")) { } // 刪除用戶 dir.Children.Remove(name); }
4. 對DirectoryEntry類的每次修改最后要調用CommitChanges()方法才能提交生效,有點EF的感覺。
5. 最難的一點是設置各類屬性,使用到了Invoke方法和Properties屬性,那Properties屬性還好,通過調試可以知道都有哪些屬性,但是可不是什么都可以通過Properties屬性來進行設置,而Invoke可就頭疼了,要自己傳入方法名,但是有哪些方法可以供調用,找了下也沒有個全面的參考手冊,這里目前我也還一知半解,最后參考文檔中列出一份,有想法的童鞋可以學習。
二、具體實例演示如何使用DirectoryEntry類來添加、修改、刪除、查詢用戶和組。
1. 添加用戶

/// <summary> /// 新增用戶 /// </summary> /// <param name="name">用戶名</param> /// <param name="password">密碼</param> /// <param name="groupName">組</param> /// <param name="description">描述</param> public void AddUser(string name, string password, string groupName, string description) { using (DirectoryEntry dir = new DirectoryEntry(_machinePath)) { using (DirectoryEntry user = dir.Children.Add(name, "User")) //增加用戶名 { user.Properties["FullName"].Add(name); //用戶全稱 user.Invoke("SetPassword", password); //用戶密碼 user.Invoke("Put", "Description", description);//用戶詳細描述 //user.Invoke("Put","PasswordExpired",1); //用戶下次登錄需更改密碼 user.Invoke("Put", "UserFlags", 66049); //密碼永不過期 //user.Invoke("Put", "UserFlags", 0x0040);//用戶不能更改密碼s user.CommitChanges();//保存用戶 using (DirectoryEntry grp = dir.Children.Find(groupName, "group")) { if (grp.Name != "") { grp.Invoke("Add", user.Path.ToString());//將用戶添加到某組 } } } } } /// <summary> /// 添加windows用戶組 /// </summary> /// <param name="groupName">組名稱</param> /// <param name="description">描述</param> public void AddGroup(string groupName, string description) { using (DirectoryEntry dir = new DirectoryEntry(_machinePath)) { using (DirectoryEntry group = dir.Children.Add(groupName, "group")) { group.Invoke("Put", new object[] { "Description", description }); group.CommitChanges(); } } }
2. 查詢並修改用戶信息

using (DirectoryEntry dir = new DirectoryEntry(PATH_LOCAL_MACHINE)) { var user = dir.Children.Find(name); user.Invoke("FullName", "全名");// 修改全名 user.Invoke("AccountDisabled", true);// 是否啟用 user.Invoke("SetPassword", new object[] { "123456" });// 修改密碼 user.Invoke("Put", "UserFlags", 66049); //密碼永不過期 //user.Invoke("Put","PasswordExpired",1); //用戶下次登錄需更改密碼 //user.Invoke("Put", "UserFlags", 0x0040);//用戶不能更改密碼 user.Invoke("Put", "Description", "這是描述");//用戶詳細描述 user.Rename(newName);// 重命名 user.CommitChanges();// 更改后提交才能生效 }
3. 刪除用戶

/// <summary> /// 用戶重命名 /// </summary> /// <param name="oldName"></param> /// <param name="newName"></param> public void RenameUser(string oldName, string newName) { using (var user = FindUserOrGroup(oldName)) { user.Rename(newName); } }
三、參考文檔