![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
Code
2
//这篇文章包含web上对windows用户上的一些操作,同样适用在Winform上。做个标记,要用的时候再来拿
3
using System;
4
using System.Data;
5
using System.Configuration;
6
using System.Collections;
7
using System.Web;
8
using System.Web.Security;
9
using System.Web.UI;
10
using System.Web.UI.WebControls;
11
using System.Web.UI.WebControls.WebParts;
12
using System.Web.UI.HtmlControls;
13
using System.DirectoryServices;
14![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
15
public partial class _Default : System.Web.UI.Page
16![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
17
protected void Page_Load(object sender, EventArgs e)
18![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
19![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
}
21
//列出所有用户信息
22
protected void btnListAllUser_Click(object sender, EventArgs e)
23![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
24
DirectoryEntry AD = new DirectoryEntry(@"WinNT://" + Environment.MachineName + ",computer");
25
foreach (DirectoryEntry child in AD.Children)
26![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
27
//列出所有用户信息
28
switch (child.SchemaClassName)
29![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
30
case "User":
31
try
32![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
33
//列出用户信息
34
DirectoryEntry entryUser = new DirectoryEntry("WinNT://" + Environment.MachineName + "/" + child.Name + ",User");
35
Response.Write("<br>");
36
Response.Write(" " + entryUser.Name);
37
Response.Write("<br>");
38
Response.Write(" " + " " + entryUser.Properties["Description"].Value);
39
Response.Write("<br>");
40
Response.Write("<br>");
41
Response.Write("<br>");
42![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
43
}
44
catch (Exception ex)
45![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
46
Response.Write("发生错误: " + ex.Message);
47
Response.Write("<br>");
48
}
49
finally
50![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
51![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
52
}
53
break;
54
}
55
}
56![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
57
}
58![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
59
//列出一组中的成员
60
protected void btnGroupUser_Click(object sender, EventArgs e)
61![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
62
string GroupName = "Users"; //组名
63
//string GroupName = "Administrators";
64
DirectoryEntry entryGroup = new DirectoryEntry(@"WinNT://" + Environment.MachineName + "/" + GroupName + ",Group");
65
Object members = entryGroup.Invoke("Members", null);
66
try
67![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
68
foreach (object member in (IEnumerable)members)
69![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
70
DirectoryEntry x = new DirectoryEntry(member);
71
Response.Write(x.Name + "<br>"); //用户名称
72
try
73![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
74
Response.Write(" " + " " + x.Properties["Description"].Value); //用户描述
75
Response.Write(x.Name + "<br>"); //用户名称
76
}
77
catch
78![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
79![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
80
}
81
}
82![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
83
}
84
catch (Exception ex)
85![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
86
Response.Write("发生错误: " + ex.Message + "<br>");
87
}
88
finally
89![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
90![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
91
}
92![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
93![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
94
}
95![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
96
//AD所有成员
97
protected void btnAllChildren_Click(object sender, EventArgs e)
98![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
99
DirectoryEntry AD = new DirectoryEntry(@"WinNT://" + Environment.MachineName + ",computer");
100
foreach (DirectoryEntry child in AD.Children)
101![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
102![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
103
//这里会列出所有组和服务的信息
104
Response.Write(child.Name);
105
Response.Write("<br>");
106
Response.Write(child.SchemaClassName);
107
Response.Write("<br>");
108
Response.Write("<br>");
109
}
110![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
111
}
112![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
113
//添加用户
114
protected void btnAddUser_Click(object sender, EventArgs e)
115![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
116
try
117![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
118
DirectoryEntry AD = new DirectoryEntry(@"WinNT://" + Environment.MachineName + ",computer");
119
//添加用户,用户名:NewUser
120
DirectoryEntry NewUser = AD.Children.Add("NewUser", "User");
121
//设置密码,密码:mypassword
122![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
NewUser.Invoke("SetPassword", new object[]
{ "mypassword" });
123![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
NewUser.Invoke("Put", new object[]
{ "Description", "myDescription" });
124
//提交修改
125
NewUser.CommitChanges();
126![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
127
//将用户添加到users组
128![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
object[] objNewUser = new object[]
{ NewUser.Path };
129
DirectoryEntry groupUser = AD.Children.Find("Users", "group");
130
groupUser.Invoke("Add", objNewUser);
131
}
132
catch (Exception ex)
133![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
134
Response.Write("添加用户时发生错误:" + ex.Message + "<br>");
135![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
136
}
137
}
138![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
139
//修改用户密码
140
protected void btnChangpwd_Click(object sender, EventArgs e)
141![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
142
DirectoryEntry AD = new DirectoryEntry(@"WinNT://" + Environment.MachineName + ",computer");
143
DirectoryEntry Cuser = AD.Children.Find("NewUser");
144
try
145![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
146![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Cuser.Invoke("SetPassword", new object[]
{ "myNewpassword" });
147
Cuser.CommitChanges();
148
}
149
catch (Exception ex)
150![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
151
Response.Write("修改密码时发生错误:" + ex.Message + "<br>");
152
}
153
}
154![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
155
//把用户从组中移除
156
protected void btnRemoveUserfromGroup_Click(object sender, EventArgs e)
157![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
158
DirectoryEntry AD = new DirectoryEntry(@"WinNT://" + Environment.MachineName + ",computer");
159
DirectoryEntry entryUser = AD.Children.Find("NewUser", "User");
160![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
object[] objUser = new object[]
{ entryUser.Path };
161
try
162![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
163
//查找users组
164
DirectoryEntry grpUsers = AD.Children.Find("Users", "group");
165
//从User组中移除
166
grpUsers.Invoke("remove", objUser);
167
}
168
catch (Exception ex)
169![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
170
Response.Write("将用户从组中移除时发生错误:" + ex.Message + "<br>");
171
}
172
}
173![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
174
//删除用户
175
protected void btnDeleteUser_Click(object sender, EventArgs e)
176![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
177
DirectoryEntry AD = new DirectoryEntry(@"WinNT://" + Environment.MachineName + ",computer");
178
try
179![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
180
DirectoryEntry ChildUser = AD.Children.Find("NewUser", "User");
181
if (ChildUser.Name != null && ChildUser.Name != "")
182![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
183
AD.Children.Remove(ChildUser);
184
}
185
}
186
catch (Exception ex)
187![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
188
Response.Write("将用户删除时发生错误:" + ex.Message + "<br>");
189
}
190
}
191
}
192![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
193
文章来源:http://ejimgao.blog.163.com/blog/static/4203001620077316102569
194![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
posted @
2007-08-31 06:10
Jim Gao
阅读(
140)
评论()
编辑
收藏
举报