在web上对windows用户进行操作

最近把以前工作中用到的一些技术点总结了一下,写了下来
None.gif using  System;
None.gif
using  System.Data;
None.gif
using  System.Configuration;
None.gif
using  System.Collections;
None.gif
using  System.Web;
None.gif
using  System.Web.Security;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.WebControls.WebParts;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
using  System.DirectoryServices;
None.gif
None.gif
public  partial  class  _Default : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif    
//列出所有用户信息
InBlock.gif
    protected void btnListAllUser_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        DirectoryEntry AD 
= new DirectoryEntry(@"WinNT://" + Environment.MachineName + ",computer");
InBlock.gif        
foreach (DirectoryEntry child in AD.Children)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//列出所有用户信息
InBlock.gif
            switch (child.SchemaClassName)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
case "User":
InBlock.gif                    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
//列出用户信息
InBlock.gif
                        DirectoryEntry entryUser = new DirectoryEntry("WinNT://" + Environment.MachineName + "/" + child.Name + ",User");
InBlock.gif                        Response.Write(
"<br>");
InBlock.gif                        Response.Write(
"&nbsp;" + entryUser.Name);
InBlock.gif                        Response.Write(
"<br>");
InBlock.gif                        Response.Write(
"&nbsp;" + "&nbsp;" + entryUser.Properties["Description"].Value);
InBlock.gif                        Response.Write(
"<br>");
InBlock.gif                        Response.Write(
"<br>");
InBlock.gif                        Response.Write(
"<br>");
InBlock.gif
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        Response.Write(
"发生错误:&nbsp;" + ex.Message);
InBlock.gif                        Response.Write(
"<br>");
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
break;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
//列出一组中的成员
InBlock.gif
    protected void btnGroupUser_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
string GroupName = "Users"//组名
InBlock.gif        
//string GroupName = "Administrators";
InBlock.gif
        DirectoryEntry entryGroup = new DirectoryEntry(@"WinNT://" + Environment.MachineName + "/" + GroupName + ",Group");
InBlock.gif        Object members 
= entryGroup.Invoke("Members"null);
InBlock.gif        
try
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
foreach (object member in (IEnumerable)members)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                DirectoryEntry x 
= new DirectoryEntry(member);
InBlock.gif                Response.Write(x.Name 
+ "<br>");                //用户名称
InBlock.gif
                try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Response.Write(
"&nbsp;" + "&nbsp;" + x.Properties["Description"].Value);    //用户描述
InBlock.gif
                    Response.Write(x.Name + "<br>");                //用户名称
ExpandedSubBlockEnd.gif
                }

InBlock.gif                
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif        
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Response.Write(
"发生错误:&nbsp;" + ex.Message + "<br>");
ExpandedSubBlockEnd.gif        }

InBlock.gif        
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
//AD所有成员
InBlock.gif
    protected void btnAllChildren_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        DirectoryEntry AD 
= new DirectoryEntry(@"WinNT://" + Environment.MachineName + ",computer");
InBlock.gif        
foreach (DirectoryEntry child in AD.Children)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            
//这里会列出所有组和服务的信息
InBlock.gif
            Response.Write(child.Name);
InBlock.gif            Response.Write(
"<br>");
InBlock.gif            Response.Write(child.SchemaClassName);
InBlock.gif            Response.Write(
"<br>");
InBlock.gif            Response.Write(
"<br>");
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
//添加用户
InBlock.gif
    protected void btnAddUser_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
try
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DirectoryEntry AD 
= new DirectoryEntry(@"WinNT://" + Environment.MachineName + ",computer");
InBlock.gif            
//添加用户,用户名:NewUser
InBlock.gif
            DirectoryEntry NewUser = AD.Children.Add("NewUser""User");
InBlock.gif            
//设置密码,密码:mypassword
ExpandedSubBlockStart.gifContractedSubBlock.gif
            NewUser.Invoke("SetPassword"new object[] dot.gif"mypassword" });
ExpandedSubBlockStart.gifContractedSubBlock.gif            NewUser.Invoke(
"Put"new object[] dot.gif"Description""myDescription" });
InBlock.gif            
//提交修改
InBlock.gif
            NewUser.CommitChanges();
InBlock.gif
InBlock.gif            
//将用户添加到users组
ExpandedSubBlockStart.gifContractedSubBlock.gif
            object[] objNewUser = new object[] dot.gif{ NewUser.Path };
InBlock.gif            DirectoryEntry groupUser 
= AD.Children.Find("Users""group");
InBlock.gif            groupUser.Invoke(
"Add", objNewUser);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Response.Write(
"添加用户时发生错误:" + ex.Message + "<br>");
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
//修改用户密码
InBlock.gif
    protected void btnChangpwd_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        DirectoryEntry AD 
= new DirectoryEntry(@"WinNT://" + Environment.MachineName + ",computer");
InBlock.gif        DirectoryEntry Cuser 
= AD.Children.Find("NewUser");
InBlock.gif        
try
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            Cuser.Invoke(
"SetPassword"new object[] dot.gif"myNewpassword" });
InBlock.gif            Cuser.CommitChanges();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Response.Write(
"修改密码时发生错误:" + ex.Message + "<br>");
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
//把用户从组中移除
InBlock.gif
    protected void btnRemoveUserfromGroup_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        DirectoryEntry AD 
= new DirectoryEntry(@"WinNT://" + Environment.MachineName + ",computer");
InBlock.gif        DirectoryEntry entryUser 
= AD.Children.Find("NewUser""User");
ExpandedSubBlockStart.gifContractedSubBlock.gif        
object[] objUser = new object[] dot.gif{ entryUser.Path };
InBlock.gif        
try
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//查找users组
InBlock.gif
            DirectoryEntry grpUsers = AD.Children.Find("Users""group");
InBlock.gif            
//从User组中移除
InBlock.gif
            grpUsers.Invoke("remove", objUser);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Response.Write(
"将用户从组中移除时发生错误:" + ex.Message + "<br>");
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
//删除用户
InBlock.gif
    protected void btnDeleteUser_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        DirectoryEntry AD 
= new DirectoryEntry(@"WinNT://" + Environment.MachineName + ",computer");
InBlock.gif        
try
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DirectoryEntry ChildUser 
= AD.Children.Find("NewUser""User");
InBlock.gif            
if (ChildUser.Name != null && ChildUser.Name != "")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                AD.Children.Remove(ChildUser);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Response.Write(
"将用户删除时发生错误:" + ex.Message + "<br>");
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/zhuhee/archive/2006/09/07/497264.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值