在命名空间System.DirectoryServeces下,DirectoryEntry类封装了Active Directory层次结构中节点或对象
首先构造一个DirectoryEntry实例,将它的Path设为"WinNT",然后通过对它的所有子项的枚举来网络上的所有域(以及工作组),再对所发现的域(心及工作组)的子项再次枚举就可以获得局域网上的所有计算机
首先构造一个DirectoryEntry实例,将它的Path设为"WinNT",然后通过对它的所有子项的枚举来网络上的所有域(以及工作组),再对所发现的域(心及工作组)的子项再次枚举就可以获得局域网上的所有计算机
using System.DirectoryServices;
private void Form1_Load(object sender, EventArgs e)
{
DirectoryEntry root = new DirectoryEntry("WinNT:");
foreach (DirectoryEntry domain in root.Children)
{
this.listBox1.Items.Add("所在域:" + domain.Name);
foreach (DirectoryEntry computer in domain.Children)
{
if (computer.Name != "Schema") // Schema为结束标记
listBox1.Items.Add("计算机:" + computer.Name);
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
DirectoryEntry root = new DirectoryEntry("WinNT:");
foreach (DirectoryEntry domain in root.Children)
{
this.listBox1.Items.Add("所在域:" + domain.Name);
foreach (DirectoryEntry computer in domain.Children)
{
if (computer.Name != "Schema") // Schema为结束标记
listBox1.Items.Add("计算机:" + computer.Name);
}
}
}
本文介绍了一种使用C#和System.DirectoryServices命名空间中的DirectoryEntry类来枚举局域网上所有计算机的方法。通过设置DirectoryEntry实例的路径并遍历其子项,可以获取到网络上的所有域和工作组,进而得到局域网内的所有计算机名称。
312

被折叠的 条评论
为什么被折叠?



