System.DirectoryServices.DirectoryEntry组件提供了对Active Directory的访问也就是你要的,我发个使用的例子
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 {}
}
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 {}
}