Use the directoryentry component to view the network and display the list of Domain Users

Source: Internet
Author: User
Tags in domain

Summary

The system. directoryservices. directoryentry component provides access to active directory. This article takes two simple applets as an example to illustrate how to use this component to view the information of each node in the network.

Directory

Question proposal

Preliminary Solution

Improved Windows Forms Solution

Summary

References

Notes

Author

Question proposal

When I first came into contact with. NET network programming, I often thought, is there a way to list all the computers in the lan? The answer was not found until I recently read the introduction to the directoryentry class in msdn.

Preliminary Solution

The directoryentry component provides the path attribute. According to the document, this attribute specifies the object name used to access the object in Directory Service. The format is as follows:

Protocol: // SERVERNAME: Port Number/Distinguished Name

The first part of this statement defines the Protocol to be used for access, as shown in figure

LDAP: (Lightweight Directory Access Protocol)

IIS: (provides IIS metadata to read and configure Internet infomation server)

Winnt: (provides access to Windows NT domains with very limited performance)

NDS: (provides access to the Novell Directory Service)

And so on (For details, refer to msdn ).

Accordingly, we construct a directoryentry instance and set its path to "winnt :", all the domains (and working groups) on the network are discovered through enumeration of all its subitems ). In this way, the subitem of the discovered domain (and Working Group) can be enumerated to discover all computers on the network. The following console applet demonstrates this.

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);}}}}}

Improved Windows Forms Solution

The two nested foreach loops in the above Code do not look very good, and the console display effect is not so beautiful. Next, I will make some changes to the Code and port it to winform.

Create a Windows application [C #] and add a Treeview to the form, named treeview1.

Add the following functions:

// Construct a node with the specified text, add it as a child node with the parant parameter, and return the newly constructed node private treenode addnode (treenode parant, string text) {treenode node = new treenode (text); parant. nodes. add (node); Return node;} // recursively locate all subnodes of the entry parameter and display it in treeview1; the entry and entrynode must correspond to private void enumchildren (directoryentry, treenode entrynode) {If (entry. children! = NULL) // end {foreach (directoryentry I in entry. children) {// Add each subnode to the Treeview and perform recursive enumchildren (I, addnode (entrynode, I. name) ;}}// construct the root node with the given string and list all its subnodes 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 {}}

In this way, by passing "winnt:" To the enumerate (string) function, you can see all the computers on the network in the Treeview, and the user, group, service, and other resources on each computer.

Summary

This article describes how to use the directoryentry component to view the information of computers on each node in the network. In fact, the directoryentry component is powerful, for example, "IIS:" is used as the path attribute of directoryentry, you can list the servers running IIS (Internet infomation server) in the domain and obtain attributes such as IIS metadata. In addition, you can also use it to remotely manage and configure the network, if you are interested, try again.

References

Windows Forms advanced programming wrox press, Tsinghua University Press

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.