利用DirectoryEntry组件来查看网络

来源:互联网 发布:淘宝保健品类目 编辑:程序博客网 时间:2024/05/16 04:41
利用DirectoryEntry组件来查看网络

摘要
System.DirectoryServices.DirectoryEntry组件提供了对Active Directory的访问。本文以两个简单的小程序为例,阐述了如何利用此组件查看网络的各节点的信息。

目录
问题的提出
问题的初步解决
改进后的Windows Forms方案
总结
参考文献
附注
作者

问题的提出
刚接触.Net网络编程的时候,我常想,有没有办法列出局域网中的所有计算机呢?直到最近看了MSDN中关于DirectoryEntry 类的介绍,这才找到了答案。

问题的初步解决
DirectoryEntry组件提供了Path属性,根据文档,此属性指定了目录服务中用来访问对象的对象名,其格式如下:
protocol://servername:port number/distinguished name
此语句的第一部分定义了访问将使用的协议,如
LDAP: (Lightweight Directory Access Protocol)
IIS: (提供IIS元数据来读及配置Internet Infomation Server)
WinNT: (提供在非常有限的性能下对Windows NT域的访问)
NDS: (提供对Novell Directory Service的访问)
等等(详细信息清参考MSDN)。
据此,我们构造了一个DirectoryEntry实例,将它的Path设为"WinNT:",以通过对它的所有子项的枚举来发现网络上的所有域(以及工作组)。这样,再对所发现的域(以及工作组)的子项进行枚举,就可以发现网络上的所有计算机。下面的一个控制台小程序演示了这一点。
using System;using System.DirectoryServices;class TempClass{static void Main(){EnumComputers();}static void EnumComputers(){using(DirectoryEntry root = new DirectoryEntry("WinNT:")){foreach(DirectoryEntry domain in root.Children){Console.WriteLine("Domain | WorkGroup:/t"+domain.Name);foreach(DirectoryEntry computer in domain.Children){Console.WriteLine("Computer:/t"+computer.Name);}}}}}

改进后的Windows Forms方案
上面代码中两个嵌套的foreach循环看起来并不是太好,并且控制台的显示效果也并不那么美观。下面,我将对代码进行一些改动,并将它移植到WinForm上。
新建一个Windows Application[C#],在Form上添加一个TreeView,命名为treeView1。
添加以下几个函数:
//用指定的文本构造一个节点,将其添加为参数parant的子节点,并返回刚构造的节点private TreeNode AddNode(TreeNode parant,string text){TreeNode node = new TreeNode(text);parant.Nodes.Add(node);return node;}//递归地找到参数entry的所有子节点,并在treeView1中显示;这里的entry与entryNode需相对应private void EnumChildren(DirectoryEntry entry,TreeNode entryNode){if(entry.Children!=null)//如果无子节点则结束{foreach(DirectoryEntry i in entry.Children){//将各子节点加入TreeView,并进行递归EnumChildren(i,AddNode(entryNode,i.Name));}}}//用给定的字符串构造根节点,并列出其所有子节点private void Enumerate(string path){try {using(DirectoryEntry root = new DirectoryEntry(path)){TreeNode node = new TreeNode(root.Name);treeView1.Nodes.Add(node);EnumChildren(root,node);}}catch {}}
这样,通过传递 "WinNT:" 给函数Enumerate(string),就可以在TreeView中看到网络上的所有计算机,以及每台计算机上的用户、组、服务等资源,效果如图:


总结
本文主要介绍了用DirectoryEntry组件来浏览网络中的各节点计算机的信息,实际上,DirectoryEntry组件功能强大,例如将"IIS:"作为DirectoryEntry的Path属性,就可以列出域中运行着IIS(Internet Infomation Server)的服务器,并可获得IIS元数据等属性;此外,还可以用它来对网络进行远程管理与配置,有兴趣者不妨一试。

参考文献
Windows Forms 高级编程 Wrox Press,清华大学出版社

附注
如果你编译并运行了第一个例子(记得添加对System.DirectoryServices.dll的引用),你会发现它会在列出计算机名的同时,还输出了
Computer: Schema
这并不是出了什么错误,对这个叫做Schema的DirectoryEntry得子项进行枚举可以发现,它正如其名,描述了Computer项的模式。当然,为了结果的有效性,我们可以滤掉它。
 
 
 
 

在OU下添加用户(user),及移除用户

添加用户:  
  DirectoryEntry   entry=new   DirectoryEntry(OUPath,(string)conns["username"],(string)conns["password"]);  
  DirectoryEntry   newEntry;  
  string   entryName="cn=userName";  
  newEntry=entry.Children.Add(entryName,"user");  
  newEntry.UsePropertyCache=true;  
  newEntry.Properties["sAMAccountName"].Value="userName";  
  newEntry.Properties["userPrincipalName"].Value=="userName";  
  newEntry.Properties["sn"].Value="usetsn";  
  newEntry.Properties["displayName"].Value="userName";  
  newEntry.Properties["userAccountControl"].Value="66048";  
  newEntry.Properties["givenName"].Value="givename";  
  newEntry.CommitChanges();  
 
 
移除用户:  
  DirectoryEntry   myds=new   DirectoryEntry(OUPath,"username","password");  
  foreach(DirectoryEntry   tempEntry   in   myds.Children)  
  {  
          if(tempEntry.SchemaClassName.ToString()   ==   "user")  
  {  
  if(tempEntry.Properties["sAMAccountName"].Value.ToString().ToLower()=="userID")  
          {  
  myds.Children.Remove(tempEntry);  
  myds.CommitChanges();  
              }  
                      }  
  }
原创粉丝点击