5,699,431 members and growing! (20,569 online)
Email Password   helpLost your password?
General Programming » Internet / Network » Network     Intermediate License: The Code Project Open License (CPOL)

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

By H. S. Masud

A collection class for listing all the computers and servers in your network, with category information.
C#, .NET, Win2K, WinXP, Win2003, Windows, Visual Studio, Dev

Posted: 7 May 2006
Updated: 20 Sep 2006
Views: 30,156
Bookmarked: 52 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
17 votes for this Article.
Popularity: 5.57 Rating: 4.53 out of 5
2 votes, 11.8%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
4 votes, 23.5%
4
11 votes, 64.7%
5

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.

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.

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)

About the Author

H. S. Masud



Occupation: Software Developer (Senior)
Company: KAZ Software Limited
Location: Bangladesh Bangladesh

Other popular Internet / Network articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 13 of 13 (Total in Forum: 13) (Refresh)FirstPrevNext
GeneralI'm developing an application similar to your's. Need helpmemberVivek Vorani3:13 24 Aug '08  
QuestionDraw a network mapmemberRadu_2022:46 23 Apr '08  
GeneralVery interesting code.memberABlokha778:21 15 Nov '06  
GeneralRe: Very interesting code.memberH. S. Masud18:55 15 Nov '06  
Generalbroken linkmembersubai19:21 25 Sep '06  
GeneralNice class!memberphilipg3623:47 1 Aug '06  
GeneralRe: Nice class!memberH. S. Masud1:43 2 Aug '06  
GeneralDoesn't workmemberNinjaCross23:25 7 May '06  
AnswerRe: Doesn't workmemberH. S. Masud23:49 7 May '06  
GeneralNow it works !memberNinjaCross0:36 8 May '06  
AnswerRe: Now it works !memberH. S. Masud1:36 8 May '06  
JokeRe: Now it works !memberH. S. Masud22:52 8 May '06  
GeneralRe: Now it works !memberNinjaCross1:13 9 May '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 20 Sep 2006
Editor: Smitha Vijayan
Copyright 2006 by H. S. Masud
Everything else Copyright © CodeProject, 1999-2008
Web18 | Advertise on the Code Project