Click here to Skip to main content
15,867,568 members
Articles / Programming Languages / C#
Article

A collection class for listing all the computers and servers in your network, with category information

Rate me:
Please Sign up or sign in to vote.
4.74/5 (20 votes)
20 Sep 2006CPOL3 min read 87.6K   3.8K   77   14
A collection class for listing all the computers and servers in your network, with category information.

Sample Image - NetworkComputers.jpg

Introduction

This article is about a collection class of all the computers in your network including domain, domain controllers, printer servers, SQL Servers, time servers, terminal servers etc.

Background

I was planning to write a LAN Chat Messenger application using remoting. In that application, I needed to know all the workstations in my domain. So, I looked at CodeProject and found a very good control titled, Network computer picker control written by Marc Merritt. Thanks to Marc Merritt for posting such a good control. When I read his article, I felt that a collection class would be more useful then a computer picker control. So, I used his code and made this collection class. I found it's great to use in my LAN Chat Messenger application.

How to use the network computers class

There are four classes in the library.

  1. DomainCollection: It's a collection of Domain classes. It has only one constructor. If the user creates an instance of this class, then it doesn't start to search the domains at the time of construction. When the DomainCollection class is used for the first time, then it searches for the domain in the network. The user can force the class to search by calling the Refresh method. This method clears the existing list, and searches for domains in the network again.
  2. Domain: The Domain class contains a collection of Computer classes. It has the following collections:
    1. Workstations: Contains all the workstations in the domain.
    2. DomainControllers: Contains all the domain controllers in the domain.
    3. TerminalServers: Contains the collection of terminal servers in the domain.
    4. TimeServers: Contains the collection of time servers in the domain.
    5. PrintServers: Contains the collection of print servers in the domain.
    6. DialinServers: Contains the collection of dial-in servers in the domain.
    7. SQLServers: Contains the collection of SQL Servers in the domain.
  3. ComputerCollection: It contains the collection of Computer classes of a specific type. It has the same behavior as the DomainCollection class. So, it doesn't search for the computers in the network at the time of construction of the class. When it is first used, it searches for the computers in the network. So, when an instance of the Domain class is created, it doesn't search for all the computers and servers in the network. When a collection, say Workstations, is used, then it searches for the workstations in the network. The user can force to refresh the list by calling the Refresh method.
  4. Computer: It has two properties: Name and ServerType.

Test Application

It is very easy to use this collection class. Here is an example of how to use the code. I hope you would enjoy this class.

The LoadComputerList method is called in a different thread from the UI of the test application. This method is not needed to use the library. The library searches the computers and servers in the network while it is first invoked. But the user may want to force the library to search domains and computers or servers, as the nature of the application demands. In this application, I search for the computers and servers in a different thread. Because, in a big network, it takes time to search. A mutex is used to identify if the search is finished.

C#
private void LoadComputerList()
{
    Mutex loadMutex=new Mutex(false,"NetworkComputer");
    loadMutex.WaitOne();
    myDomains=new DomainCollection();
    myDomains.Refresh();
    for (int i=0;i<myDomains.Count;i++)
    {
        myDomains[i].DialinServers.Refresh();
        myDomains[i].DomainControllers.Refresh();
        myDomains[i].PrintServers.Refresh();
        myDomains[i].TerminalServers.Refresh();
        myDomains[i].TimeServers.Refresh();
        myDomains[i].Workstations.Refresh();
        myDomains[i].SQLServers.Refresh();
    }
    loadMutex.ReleaseMutex();
}

To load the list in a tree view, I use a timer where I wait for the mutex.

C#
private void timer1_Tick(object sender, System.EventArgs e)
{
    Mutex loadMutex=new Mutex(false,"NetworkComputer");
    if(loadMutex.WaitOne(0,false)==true)
    {
        timer1.Enabled=false;
        tvComputerList.Nodes.Clear();
        for (int i=0;i<myDomains.Count;i++)
        {
            System.Windows.Forms.TreeNode domainNode= 
                           new TreeNode(myDomains[i].Name);
            tvComputerList.Nodes.Add(domainNode);
            if (myDomains[i].DialinServers.Count>0)
            {
                System.Windows.Forms.TreeNode serversNode=
                           new TreeNode("Dial In Servers");
                domainNode.Nodes.Add(serversNode);
                for (int j=0;j<myDomains[i].DialinServers.Count;j++)
                {
                    System.Windows.Forms.TreeNode computerNode=
                           new TreeNode(myDomains[i].DialinServers[j].Name);
                    serversNode.Nodes.Add(computerNode);
                }
            }
            if (myDomains[i].DomainControllers.Count>0)
            {
                System.Windows.Forms.TreeNode serversNode=
                               new TreeNode("Domain Controllers");
                domainNode.Nodes.Add(serversNode);
                for (int j=0;j<myDomains[i].DomainControllers.Count;j++)
                {
                    System.Windows.Forms.TreeNode computerNode=
                           new TreeNode(myDomains[i].DomainControllers[j].Name);
                    serversNode.Nodes.Add(computerNode);
                }
            }
            if (myDomains[i].PrintServers.Count>0)
            {
                System.Windows.Forms.TreeNode serversNode=
                            new TreeNode("Print Servers");
                domainNode.Nodes.Add(serversNode);
                for (int j=0;j<myDomains[i].PrintServers.Count;j++)
                {
                    System.Windows.Forms.TreeNode computerNode=
                      new TreeNode(myDomains[i].PrintServers[j].Name);
                    serversNode.Nodes.Add(computerNode);
                }
            }
            if (myDomains[i].TerminalServers.Count>0)
            {
                System.Windows.Forms.TreeNode serversNode=
                         new TreeNode("Terminal Servers");
                domainNode.Nodes.Add(serversNode);
                for (int j=0;j<myDomains[i].TerminalServers.Count;j++)
                {
                    System.Windows.Forms.TreeNode computerNode=
                      new TreeNode(myDomains[i].TerminalServers[j].Name);
                    serversNode.Nodes.Add(computerNode);
                }
            }
            if (myDomains[i].TimeServers.Count>0)
            {
                System.Windows.Forms.TreeNode serversNode=
                     new TreeNode("Time Servers");
                domainNode.Nodes.Add(serversNode);
                for (int j=0;j<myDomains[i].TimeServers.Count;j++)
                {
                    System.Windows.Forms.TreeNode computerNode=
                           new TreeNode(myDomains[i].TimeServers[j].Name);
                    serversNode.Nodes.Add(computerNode);
                }
            }
            if (myDomains[i].Workstations.Count>0)
            {
                System.Windows.Forms.TreeNode serversNode=
                             new TreeNode("Workstations");
                domainNode.Nodes.Add(serversNode);
                for (int j=0;j<myDomains[i].Workstations.Count;j++)
                {
                    System.Windows.Forms.TreeNode computerNode=
                      new TreeNode(myDomains[i].Workstations[j].Name);
                    serversNode.Nodes.Add(computerNode);
                }
            }
            if (myDomains[i].SQLServers.Count>0)
            {
                System.Windows.Forms.TreeNode serversNode=
                              new TreeNode("SQL Servers");
                domainNode.Nodes.Add(serversNode);
                for (int j=0;j<myDomains[i].SQLServers.Count;j++)
                {
                    System.Windows.Forms.TreeNode computerNode=
                           new TreeNode(myDomains[i].SQLServers[j].Name);
                    serversNode.Nodes.Add(computerNode);
                }
            }
        }
        loadMutex.ReleaseMutex();
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) KAZ Software Limited
Bangladesh Bangladesh
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
ytfrdfiw31-Oct-12 19:13
ytfrdfiw31-Oct-12 19:13 
GeneralI'm developing an application similar to your's. Need help Pin
Vivek Vorani24-Aug-08 2:13
Vivek Vorani24-Aug-08 2:13 
QuestionDraw a network map Pin
Radu_2023-Apr-08 21:46
Radu_2023-Apr-08 21:46 
GeneralVery interesting code. Pin
ABlokha7715-Nov-06 7:21
ABlokha7715-Nov-06 7:21 
GeneralRe: Very interesting code. Pin
H. S. Masud15-Nov-06 17:55
H. S. Masud15-Nov-06 17:55 
Generalbroken link Pin
subai25-Sep-06 18:21
subai25-Sep-06 18:21 
GeneralNice class! Pin
philipg361-Aug-06 22:47
philipg361-Aug-06 22:47 
GeneralRe: Nice class! Pin
H. S. Masud2-Aug-06 0:43
H. S. Masud2-Aug-06 0:43 
GeneralDoesn't work Pin
NinjaCross7-May-06 22:25
NinjaCross7-May-06 22:25 
AnswerRe: Doesn't work Pin
H. S. Masud7-May-06 22:49
H. S. Masud7-May-06 22:49 
GeneralNow it works ! Pin
NinjaCross7-May-06 23:36
NinjaCross7-May-06 23:36 
AnswerRe: Now it works ! Pin
H. S. Masud8-May-06 0:36
H. S. Masud8-May-06 0:36 
JokeRe: Now it works ! Pin
H. S. Masud8-May-06 21:52
H. S. Masud8-May-06 21:52 
GeneralRe: Now it works ! Pin
NinjaCross9-May-06 0:13
NinjaCross9-May-06 0:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.