下面的代码是在平时工作中收集而来,贴在这里方便大家不时之需。
1 /// <summary>
2 /// 创建本地用户
3 /// </summary>
4 /// <param name="userName">用户名</param>
5 /// <param name="userPassword">用户密码</param>
6 /// <param name="msg">返回消息</param>
7 /// <returns>成功与否</returns>
8 public static bool CreateLocalUserAccount(string userName, string userPassword, out string returnMsg)
9 {
10 bool bRet = false;
11 returnMsg = "";
12 DirectoryEntry oLocalMachine = null;
13 DirectoryEntry oNewUser = null;
14 try
15 {
16 oLocalMachine = new DirectoryEntry("WinNT://" + Environment.MachineName);
17 oNewUser = oLocalMachine.Children.Find(userName, "user");
18 }
19 catch
20 {
21
22 }
23 try
24 {
25 if (oNewUser == null)
26 {
27 oNewUser = oLocalMachine.Children.Add(userName, "user");
28 oNewUser.CommitChanges();
29 returnMsg = oNewUser.Guid.ToString();
30 oNewUser.Invoke("SetPassword", new object[] { userPassword });
31 oNewUser.CommitChanges();
32 oNewUser.Invoke("Put", "UserFlags", DONT_EXPIRE_PASSWORD);
33 oNewUser.CommitChanges();
34 oLocalMachine.Close();
35 oNewUser.Close();
36 bRet = true;
37 }
38 else
39 {
40 bRet = true;
41 returnMsg = string.Format("用户[{0}]已存在,无法重复创建改该用户!", userName);
42 }
43 }
44 catch (Exception ex)
45 {
46 returnMsg = ex.Message;
47 }
48 finally
49 {
50 if (oLocalMachine != null)
51 {
52 oLocalMachine.Close();
53 oLocalMachine.Dispose();
54 oLocalMachine = null;
55 }
56 if (oNewUser != null)
57 {
58 oNewUser.Close();
59 oNewUser.Dispose();
60 oNewUser = null;
61 }
62 }
63 return bRet;
64 }
2 /// 创建本地用户
3 /// </summary>
4 /// <param name="userName">用户名</param>
5 /// <param name="userPassword">用户密码</param>
6 /// <param name="msg">返回消息</param>
7 /// <returns>成功与否</returns>
8 public static bool CreateLocalUserAccount(string userName, string userPassword, out string returnMsg)
9 {
10 bool bRet = false;
11 returnMsg = "";
12 DirectoryEntry oLocalMachine = null;
13 DirectoryEntry oNewUser = null;
14 try
15 {
16 oLocalMachine = new DirectoryEntry("WinNT://" + Environment.MachineName);
17 oNewUser = oLocalMachine.Children.Find(userName, "user");
18 }
19 catch
20 {
21
22 }
23 try
24 {
25 if (oNewUser == null)
26 {
27 oNewUser = oLocalMachine.Children.Add(userName, "user");
28 oNewUser.CommitChanges();
29 returnMsg = oNewUser.Guid.ToString();
30 oNewUser.Invoke("SetPassword", new object[] { userPassword });
31 oNewUser.CommitChanges();
32 oNewUser.Invoke("Put", "UserFlags", DONT_EXPIRE_PASSWORD);
33 oNewUser.CommitChanges();
34 oLocalMachine.Close();
35 oNewUser.Close();
36 bRet = true;
37 }
38 else
39 {
40 bRet = true;
41 returnMsg = string.Format("用户[{0}]已存在,无法重复创建改该用户!", userName);
42 }
43 }
44 catch (Exception ex)
45 {
46 returnMsg = ex.Message;
47 }
48 finally
49 {
50 if (oLocalMachine != null)
51 {
52 oLocalMachine.Close();
53 oLocalMachine.Dispose();
54 oLocalMachine = null;
55 }
56 if (oNewUser != null)
57 {
58 oNewUser.Close();
59 oNewUser.Dispose();
60 oNewUser = null;
61 }
62 }
63 return bRet;
64 }
1 /// <summary>
2 /// 删除本地用户
3 /// </summary>
4 /// <param name="userName">用户名</param>
5 /// <returns>成功与否</returns>
6 public static bool DeleteLocalUserAccount(string userName, out string returnMsg)
7 {
8 bool bRet = true;
9 returnMsg = "";
10 DirectoryEntry oLocalMachine = null;
11 DirectoryEntry oNewUser = null;
12 try
13 {
14 oLocalMachine = new DirectoryEntry("WinNT://" + Environment.MachineName);
15 oNewUser = oLocalMachine.Children.Find(userName, "user");
16 }
17 catch
18 {
19
20 }
21 try
22 {
23 if (oNewUser != null)
24 {
25 oLocalMachine.Children.Remove(oNewUser);
26 oLocalMachine.Close();
27 }
28 }
29 catch (Exception ex)
30 {
31 returnMsg = ex.Message;
32 bRet = false;
33 }
34 finally
35 {
36 if (oLocalMachine != null)
37 {
38 oLocalMachine.Close();
39 oLocalMachine.Dispose();
40 oLocalMachine = null;
41 }
42 if (oNewUser != null)
43 {
44 oNewUser.Close();
45 oNewUser.Dispose();
46 oNewUser = null;
47 }
48 }
49 return bRet;
50 }
2 /// 删除本地用户
3 /// </summary>
4 /// <param name="userName">用户名</param>
5 /// <returns>成功与否</returns>
6 public static bool DeleteLocalUserAccount(string userName, out string returnMsg)
7 {
8 bool bRet = true;
9 returnMsg = "";
10 DirectoryEntry oLocalMachine = null;
11 DirectoryEntry oNewUser = null;
12 try
13 {
14 oLocalMachine = new DirectoryEntry("WinNT://" + Environment.MachineName);
15 oNewUser = oLocalMachine.Children.Find(userName, "user");
16 }
17 catch
18 {
19
20 }
21 try
22 {
23 if (oNewUser != null)
24 {
25 oLocalMachine.Children.Remove(oNewUser);
26 oLocalMachine.Close();
27 }
28 }
29 catch (Exception ex)
30 {
31 returnMsg = ex.Message;
32 bRet = false;
33 }
34 finally
35 {
36 if (oLocalMachine != null)
37 {
38 oLocalMachine.Close();
39 oLocalMachine.Dispose();
40 oLocalMachine = null;
41 }
42 if (oNewUser != null)
43 {
44 oNewUser.Close();
45 oNewUser.Dispose();
46 oNewUser = null;
47 }
48 }
49 return bRet;
50 }
1 /// <summary>
2 /// 修改用户密码
3 /// </summary>
4 /// <param name="userName">用户名</param>
5 /// <returns>成功与否</returns>
6 public static bool ChangePassword(string userName, string userPassword, out string returnMsg)
7 {
8 bool bRet = true;
9 returnMsg = "";
10 DirectoryEntry oLocalMachine = null;
11 DirectoryEntry oNewUser = null;
12 try
13 {
14 oLocalMachine = new DirectoryEntry("WinNT://" + Environment.MachineName);
15 oNewUser = oLocalMachine.Children.Find(userName, "user");
16 }
17 catch
18 {
19
20 }
21 try
22 {
23 if (oNewUser != null)
24 {
25 oNewUser.Invoke("SetPassword", new object[] { userPassword });
26 oNewUser.CommitChanges();
27 }
28 else
29 {
30 returnMsg = string.Format("用户[{0}]不存在,无法修改密码!", userName);
31 bRet = false;
32 }
33 }
34 catch (Exception ex)
35 {
36 returnMsg = ex.Message;
37 bRet = false;
38 }
39 finally
40 {
41 if (oLocalMachine != null)
42 {
43 oLocalMachine.Close();
44 oLocalMachine.Dispose();
45 oLocalMachine = null;
46 }
47 if (oNewUser != null)
48 {
49 oNewUser.Close();
50 oNewUser.Dispose();
51 oNewUser = null;
52 }
53 }
54 return bRet;
55 }
2 /// 修改用户密码
3 /// </summary>
4 /// <param name="userName">用户名</param>
5 /// <returns>成功与否</returns>
6 public static bool ChangePassword(string userName, string userPassword, out string returnMsg)
7 {
8 bool bRet = true;
9 returnMsg = "";
10 DirectoryEntry oLocalMachine = null;
11 DirectoryEntry oNewUser = null;
12 try
13 {
14 oLocalMachine = new DirectoryEntry("WinNT://" + Environment.MachineName);
15 oNewUser = oLocalMachine.Children.Find(userName, "user");
16 }
17 catch
18 {
19
20 }
21 try
22 {
23 if (oNewUser != null)
24 {
25 oNewUser.Invoke("SetPassword", new object[] { userPassword });
26 oNewUser.CommitChanges();
27 }
28 else
29 {
30 returnMsg = string.Format("用户[{0}]不存在,无法修改密码!", userName);
31 bRet = false;
32 }
33 }
34 catch (Exception ex)
35 {
36 returnMsg = ex.Message;
37 bRet = false;
38 }
39 finally
40 {
41 if (oLocalMachine != null)
42 {
43 oLocalMachine.Close();
44 oLocalMachine.Dispose();
45 oLocalMachine = null;
46 }
47 if (oNewUser != null)
48 {
49 oNewUser.Close();
50 oNewUser.Dispose();
51 oNewUser = null;
52 }
53 }
54 return bRet;
55 }
1 /// <summary>
2 /// 检查用户是否存在
3 /// </summary>
4 /// <param name="user"></param>
5 /// <param name="returnMsg"></param>
6 /// <returns></returns>
7 public static bool IsUserExists(string userName)
8 {
9 bool bRet = false;
10 DirectoryEntry oLocalMachine = null;
11 DirectoryEntry oNewUser = null;
12 try
13 {
14 oLocalMachine = new DirectoryEntry("WinNT://" + Environment.MachineName);
15 oNewUser = oLocalMachine.Children.Find(userName, "user");
16 if (oNewUser != null)
17 {
18 bRet = true;
19 }
20 }
21 catch
22 {
23 bRet = false;
24 }
25 return bRet;
26 }
2 /// 检查用户是否存在
3 /// </summary>
4 /// <param name="user"></param>
5 /// <param name="returnMsg"></param>
6 /// <returns></returns>
7 public static bool IsUserExists(string userName)
8 {
9 bool bRet = false;
10 DirectoryEntry oLocalMachine = null;
11 DirectoryEntry oNewUser = null;
12 try
13 {
14 oLocalMachine = new DirectoryEntry("WinNT://" + Environment.MachineName);
15 oNewUser = oLocalMachine.Children.Find(userName, "user");
16 if (oNewUser != null)
17 {
18 bRet = true;
19 }
20 }
21 catch
22 {
23 bRet = false;
24 }
25 return bRet;
26 }
1 /// <summary>
2 /// 获取本地所有用户
3 /// </summary>
4 /// <returns></returns>
5 public static List<string> GetLocalUserAccount(out string returnMsg)
6 {
7 List<string> oRet = new List<string>();
8 DirectoryEntry oLocalMachine = null;
9 DirectoryEntry oEntry = null;
10 try
11 {
12 returnMsg = null;
13 oLocalMachine = new DirectoryEntry("WinNT://" + Environment.MachineName);
14 IEnumerator oUserEnum = oLocalMachine.Children.GetEnumerator();
15 while (oUserEnum.MoveNext())
16 {
17 oEntry = oUserEnum.Current as DirectoryEntry;
18 if (oEntry != null)
19 {
20 if (oEntry.SchemaClassName.Equals("user", StringComparison.OrdinalIgnoreCase))
21 {
22 oRet.Add(oEntry.Name);
23 }
24 oEntry.Close();
25 oEntry.Dispose();
26 }
27 }
28 }
29 catch (Exception ex)
30 {
31 returnMsg = ex.Message;
32 }
33 finally
34 {
35 if (oLocalMachine != null)
36 {
37 oLocalMachine.Close();
38 oLocalMachine.Dispose();
39 oLocalMachine = null;
40 }
41 }
42 return oRet;
43 }
2 /// 获取本地所有用户
3 /// </summary>
4 /// <returns></returns>
5 public static List<string> GetLocalUserAccount(out string returnMsg)
6 {
7 List<string> oRet = new List<string>();
8 DirectoryEntry oLocalMachine = null;
9 DirectoryEntry oEntry = null;
10 try
11 {
12 returnMsg = null;
13 oLocalMachine = new DirectoryEntry("WinNT://" + Environment.MachineName);
14 IEnumerator oUserEnum = oLocalMachine.Children.GetEnumerator();
15 while (oUserEnum.MoveNext())
16 {
17 oEntry = oUserEnum.Current as DirectoryEntry;
18 if (oEntry != null)
19 {
20 if (oEntry.SchemaClassName.Equals("user", StringComparison.OrdinalIgnoreCase))
21 {
22 oRet.Add(oEntry.Name);
23 }
24 oEntry.Close();
25 oEntry.Dispose();
26 }
27 }
28 }
29 catch (Exception ex)
30 {
31 returnMsg = ex.Message;
32 }
33 finally
34 {
35 if (oLocalMachine != null)
36 {
37 oLocalMachine.Close();
38 oLocalMachine.Dispose();
39 oLocalMachine = null;
40 }
41 }
42 return oRet;
43 }
1 /// <summary>
2 /// 向用户组添加用户
3 /// </summary>
4 /// <param name="userName">用户名</param>
5 /// <param name="groupName">用户组名</param>
6 /// <param name="returnMsg">返回的错误消息</param>
7 /// <returns></returns>
8 public static bool AddUserToGroup(string userName, string groupName, out string returnMsg)
9 {
10 bool bRet = false;
11 returnMsg = "";
12 DirectoryEntry oLocalMachine = null;
13 DirectoryEntry oGroup = null;
14 DirectoryEntry oNewUser = null;
15 try
16 {
17 oLocalMachine = new DirectoryEntry("WinNT://" + Environment.MachineName);
18 oGroup = oLocalMachine.Children.Find(groupName, "group");
19 oNewUser = oLocalMachine.Children.Find(userName, "user");
20 }
21 catch
22 { }
23 try
24 {
25 if (oGroup != null)
26 {
27 if (oNewUser != null)
28 {
29 if (!IsUserInGroups(oNewUser, groupName))
30 {
31 oGroup.Invoke("Add", new object[] { oNewUser.Path });
32 oGroup.CommitChanges();
33 bRet = true;
34 }
35 else
36 {
37 oGroup.Invoke("Remove", new object[] { oNewUser.Path });
38 oGroup.CommitChanges();
39 bRet = true;
40 }
41 }
42 else
43 {
44 returnMsg = string.Format("没有找到用户[{0}]", userName);
45 }
46 }
47 else
48 {
49 returnMsg = string.Format("没有找到用户组[{0}]", groupName);
50 }
51 }
52 catch (Exception ex)
53 {
54 returnMsg = string.Format("往用户组[{0}]增加用户[{1}]时发生异常:\r\n{2}", groupName, userName, ex.Message);
55 }
56 finally
57 {
58 if (oLocalMachine != null)
59 {
60 oLocalMachine.Close();
61 oLocalMachine.Dispose();
62 oLocalMachine = null;
63 }
64 if (oGroup != null)
65 {
66 oGroup.Close();
67 oGroup.Dispose();
68 oGroup = null;
69 }
70 if (oNewUser != null)
71 {
72 oNewUser.Close();
73 oNewUser.Dispose();
74 oNewUser = null;
75 }
76 }
77 return bRet;
78 }
79
80 /// <summary>
81 /// 从用户组删除用户
82 /// </summary>
83 /// <param name="userName">用户名</param>
84 /// <param name="groupName">用户组名</param>
85 /// <param name="returnMsg">返回的错误消息</param>
86 /// <returns></returns>
87 public static bool RemoveUserFromGroup(string userName, string groupName, out string returnMsg)
88 {
89 bool bRet = false;
90 returnMsg = "";
91 DirectoryEntry oLocalMachine = null;
92 DirectoryEntry oGroup = null;
93 DirectoryEntry oNewUser = null;
94 try
95 {
96 oLocalMachine = new DirectoryEntry("WinNT://" + Environment.MachineName);
97 oGroup = oLocalMachine.Children.Find(groupName, "group");
98 oNewUser = oLocalMachine.Children.Find(userName, "user");
99 }
100 catch
101 { }
102 try
103 {
104 if (oGroup != null)
105 {
106 if (oNewUser != null)
107 {
108 if (IsUserInGroups(oNewUser, groupName))
109 {
110 oGroup.Invoke("Remove", new object[] { oNewUser.Path });
111 oGroup.CommitChanges();
112 }
113 bRet = true;
114 }
115 else
116 {
117 returnMsg = string.Format("没有找到用户[{0}]", userName);
118 }
119 }
120 else
121 {
122 returnMsg = string.Format("没有找到用户组[{0}]", groupName);
123 }
124 }
125 catch (Exception ex)
126 {
127 returnMsg = string.Format("从用户组[{0}]删除用户[{1}]时发生异常:\r\n{2}", groupName, userName, ex.Message);
128 }
129 finally
130 {
131 if (oLocalMachine != null)
132 {
133 oLocalMachine.Close();
134 oLocalMachine.Dispose();
135 oLocalMachine = null;
136 }
137 if (oGroup != null)
138 {
139 oGroup.Close();
140 oGroup.Dispose();
141 oGroup = null;
142 }
143 if (oNewUser != null)
144 {
145 oNewUser.Close();
146 oNewUser.Dispose();
147 oNewUser = null;
148 }
149 }
150 return bRet;
151 }
152
153 /// <summary>
154 /// 判断用户是否已在用户组中
155 /// </summary>
156 /// <param name="userObj"></param>
157 /// <param name="groupName"></param>
158 /// <returns></returns>
159 private static bool IsUserInGroups(DirectoryEntry userObj, string groupName)
160 {
161 bool bRet = false;
162 if (userObj != null)
163 {
164 object oGroups = userObj.Invoke("groups", null);
165 DirectoryEntry oGroup = null;
166 try
167 {
168 foreach (object group in (IEnumerable)oGroups)
169 {
170 oGroup = new DirectoryEntry(group);
171 if (oGroup.Name.Equals(groupName, StringComparison.OrdinalIgnoreCase))
172 {
173 bRet = true;
174 break;
175 }
176 }
177 }
178 catch { }
179 }
180 return bRet;
181 }
2 /// 向用户组添加用户
3 /// </summary>
4 /// <param name="userName">用户名</param>
5 /// <param name="groupName">用户组名</param>
6 /// <param name="returnMsg">返回的错误消息</param>
7 /// <returns></returns>
8 public static bool AddUserToGroup(string userName, string groupName, out string returnMsg)
9 {
10 bool bRet = false;
11 returnMsg = "";
12 DirectoryEntry oLocalMachine = null;
13 DirectoryEntry oGroup = null;
14 DirectoryEntry oNewUser = null;
15 try
16 {
17 oLocalMachine = new DirectoryEntry("WinNT://" + Environment.MachineName);
18 oGroup = oLocalMachine.Children.Find(groupName, "group");
19 oNewUser = oLocalMachine.Children.Find(userName, "user");
20 }
21 catch
22 { }
23 try
24 {
25 if (oGroup != null)
26 {
27 if (oNewUser != null)
28 {
29 if (!IsUserInGroups(oNewUser, groupName))
30 {
31 oGroup.Invoke("Add", new object[] { oNewUser.Path });
32 oGroup.CommitChanges();
33 bRet = true;
34 }
35 else
36 {
37 oGroup.Invoke("Remove", new object[] { oNewUser.Path });
38 oGroup.CommitChanges();
39 bRet = true;
40 }
41 }
42 else
43 {
44 returnMsg = string.Format("没有找到用户[{0}]", userName);
45 }
46 }
47 else
48 {
49 returnMsg = string.Format("没有找到用户组[{0}]", groupName);
50 }
51 }
52 catch (Exception ex)
53 {
54 returnMsg = string.Format("往用户组[{0}]增加用户[{1}]时发生异常:\r\n{2}", groupName, userName, ex.Message);
55 }
56 finally
57 {
58 if (oLocalMachine != null)
59 {
60 oLocalMachine.Close();
61 oLocalMachine.Dispose();
62 oLocalMachine = null;
63 }
64 if (oGroup != null)
65 {
66 oGroup.Close();
67 oGroup.Dispose();
68 oGroup = null;
69 }
70 if (oNewUser != null)
71 {
72 oNewUser.Close();
73 oNewUser.Dispose();
74 oNewUser = null;
75 }
76 }
77 return bRet;
78 }
79
80 /// <summary>
81 /// 从用户组删除用户
82 /// </summary>
83 /// <param name="userName">用户名</param>
84 /// <param name="groupName">用户组名</param>
85 /// <param name="returnMsg">返回的错误消息</param>
86 /// <returns></returns>
87 public static bool RemoveUserFromGroup(string userName, string groupName, out string returnMsg)
88 {
89 bool bRet = false;
90 returnMsg = "";
91 DirectoryEntry oLocalMachine = null;
92 DirectoryEntry oGroup = null;
93 DirectoryEntry oNewUser = null;
94 try
95 {
96 oLocalMachine = new DirectoryEntry("WinNT://" + Environment.MachineName);
97 oGroup = oLocalMachine.Children.Find(groupName, "group");
98 oNewUser = oLocalMachine.Children.Find(userName, "user");
99 }
100 catch
101 { }
102 try
103 {
104 if (oGroup != null)
105 {
106 if (oNewUser != null)
107 {
108 if (IsUserInGroups(oNewUser, groupName))
109 {
110 oGroup.Invoke("Remove", new object[] { oNewUser.Path });
111 oGroup.CommitChanges();
112 }
113 bRet = true;
114 }
115 else
116 {
117 returnMsg = string.Format("没有找到用户[{0}]", userName);
118 }
119 }
120 else
121 {
122 returnMsg = string.Format("没有找到用户组[{0}]", groupName);
123 }
124 }
125 catch (Exception ex)
126 {
127 returnMsg = string.Format("从用户组[{0}]删除用户[{1}]时发生异常:\r\n{2}", groupName, userName, ex.Message);
128 }
129 finally
130 {
131 if (oLocalMachine != null)
132 {
133 oLocalMachine.Close();
134 oLocalMachine.Dispose();
135 oLocalMachine = null;
136 }
137 if (oGroup != null)
138 {
139 oGroup.Close();
140 oGroup.Dispose();
141 oGroup = null;
142 }
143 if (oNewUser != null)
144 {
145 oNewUser.Close();
146 oNewUser.Dispose();
147 oNewUser = null;
148 }
149 }
150 return bRet;
151 }
152
153 /// <summary>
154 /// 判断用户是否已在用户组中
155 /// </summary>
156 /// <param name="userObj"></param>
157 /// <param name="groupName"></param>
158 /// <returns></returns>
159 private static bool IsUserInGroups(DirectoryEntry userObj, string groupName)
160 {
161 bool bRet = false;
162 if (userObj != null)
163 {
164 object oGroups = userObj.Invoke("groups", null);
165 DirectoryEntry oGroup = null;
166 try
167 {
168 foreach (object group in (IEnumerable)oGroups)
169 {
170 oGroup = new DirectoryEntry(group);
171 if (oGroup.Name.Equals(groupName, StringComparison.OrdinalIgnoreCase))
172 {
173 bRet = true;
174 break;
175 }
176 }
177 }
178 catch { }
179 }
180 return bRet;
181 }