zoukankan      html  css  js  c++  java
  • 利用DirectoryEntry组件来查看网络

    正文:

    利用DirectoryEntry组件来查看网络

    摘要

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

    问题的提出

    刚接触.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中显示;这里的entryentryNode需相对应

    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:"作为DirectoryEntryPath属性,就可以列出域中运行着IIS(Internet Infomation Server)的服务器,并可获得IIS元数据等属性;此外,还可以用它来对网络进行远程管理与配置,有兴趣者不妨一试。

    参考文献

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

    附注

    如果你编译并运行了第一个例子(记得添加对System.DirectoryServices.dll的引用),你会发现它会在列出计算机名的同时,还输出了

    Computer: Schema

    这并不是出了什么错误,对这个叫做SchemaDirectoryEntry得子项进行枚举可以发现,它正如其名,描述了Computer项的模式。当然,为了结果的有效性,我们可以滤掉它。
    类别: Active Directory
    发布日期: 2007-3-9 14:26
  • 相关阅读:
    Leetcode Substring with Concatenation of All Words
    Leetcode Divide Two Integers
    Leetcode Edit Distance
    Leetcode Longest Palindromic Substring
    Leetcode Longest Substring Without Repeating Characters
    Leetcode 4Sum
    Leetcode 3Sum Closest
    Leetcode 3Sum
    Leetcode Candy
    Leetcode jump Game II
  • 原文地址:https://www.cnblogs.com/yinpengxiang/p/1416148.html
Copyright © 2011-2022 走看看