Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

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

4.74/5 (20 votes)
20 Sep 2006CPOL3 min read 1   3.9K  
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)