Click here to Skip to main content
Click here to Skip to main content

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

By , 20 Sep 2006
 

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
Software Developer (Senior) KAZ Software Limited
Bangladesh Bangladesh
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberytfrdfiw31 Oct '12 - 19:13 
very good article
GeneralI'm developing an application similar to your's. Need helpmemberVivek Vorani24 Aug '08 - 2:13 
Dear Mr.Masud, I've a college project in which I've to develop an application which can send simple text messages to and from several 10-15 computers in a network. They have fixed IP addresses. Which, do you think is the easiest way to do this. I know that the easiest way is also a tough one, but still. I want to complete this project in just 3 months.(It's the only time we've got).
 
Awaiting your reply.
 
Vivek Vorani.
QuestionDraw a network mapmemberRadu_2023 Apr '08 - 21:46 
I saw softwares that can draw a network map. Do you have any idea how can I do that? Or do you know some example? Thanks.
 

GeneralVery interesting code.memberABlokha7715 Nov '06 - 7:21 
Very interesting code. Before I thought, that such nuctionality already exists in .net or .net 2.0.
 
But how can I retrieve the list of shares (shared folders) on workstation?
GeneralRe: Very interesting code.memberH. S. Masud15 Nov '06 - 17:55 
Hi,
Thanks for your message.
I don't know how to know the shares. But I think you will get hundreds of code in the net.
 
Hasan Shahriar Masud
Senior Software Engineer
Kaz Software limited
Dhaka, Bangladesh
URL: http://www.hsmasud.com
E-Mail: info@hsmasud.com
Company: http://www.kaz.com.bd

Generalbroken linkmembersubai25 Sep '06 - 18:21 
i think the link to "Network computer picker control" article is invalid , i tried this instead of that http://www.codeproject.com/cs/internet/comppickerlib.asp
 
I Wish the Life Had CTRL-Z

GeneralNice class!memberphilipg361 Aug '06 - 22:47 
I've tried to use your class on my project and worked really fine.
 
Thanks!!!Big Grin | :-D
GeneralRe: Nice class!memberH. S. Masud2 Aug '06 - 0:43 
You are welcome
 
Hasan Shahriar Masud
Senior Software Engineer
Kaz Software limited
Dhaka, Bangladesh
URL: http://www.hsmasud.com
E-Mail: info@hsmasud.com

GeneralDoesn't workmemberNinjaCross7 May '06 - 22:25 
Thanks so much for posting this article, i would like to say that's working like a sweet.... but unfortunatley the application's demo doesn't work Frown | :(
It seems blocked during loading and neither the main window wants to appear.
 
I tried to recompile it all and debug it, but the process stops working without apparent motivation after the loading of the "interop.sqldmo.dll" assembly.
Here you can see the Console Output:
 
'DefaultDomain': Loaded 'c:\windows\microsoft.net\framework\v1.1.4322\mscorlib.dll', No symbols loaded.
'ComputerCollectionTest': Loaded 'C:\Temp\Network computers\NetworkComputers_src\ComputerCollectionTest\bin\Release\ComputerCollectionTest.exe', No symbols loaded.
'ComputerCollectionTest.exe': Loaded 'c:\windows\assembly\gac\system.windows.forms\1.0.5000.0__b77a5c561934e089\system.windows.forms.dll', No symbols loaded.
'ComputerCollectionTest.exe': Loaded 'c:\windows\assembly\gac\system\1.0.5000.0__b77a5c561934e089\system.dll', No symbols loaded.
'ComputerCollectionTest.exe': Loaded 'c:\windows\assembly\gac\system.drawing\1.0.5000.0__b03f5f7f11d50a3a\system.drawing.dll', No symbols loaded.
'ComputerCollectionTest.exe': Loaded 'C:\Temp\network computers\networkcomputers_src\computercollectiontest\bin\release\networkcomputers.dll', No symbols loaded.
'ComputerCollectionTest.exe': Loaded 'C:\Temp\network computers\networkcomputers_src\computercollectiontest\bin\release\interop.sqldmo.dll', No symbols loaded.
 
--- the loading process stops working here, and the task manager shows that it doesn't use CPU resources.
 
I've got WinXP SP2 on a P4 @ 3GHz with MT.
Hope this helps.
 
--
NinjaCross
www.ninjacross.com
 
-- modified at 4:27 Monday 8th May, 2006
AnswerRe: Doesn't workmemberH. S. Masud7 May '06 - 22:49 
Hi,
Thanks for bug report. I'll try to solve the problem. You stil can use the library for workstation and other servers except SQL Server. I'll re-write to find SQL Server and update the library as soon as possible.
 
Thanks
 
Hasan Shahriar Masud
Senior Software Engineer
Kaz Software limited
Dhaka, Bangladesh
URL: http://www.hsmasud.com
E-Mail: info@hsmasud.com
GeneralNow it works !memberNinjaCross7 May '06 - 23:36 
Misteriously, now the program works correctly Big Grin | :-D
The only thing that i've got to warn about, is that the scanning is a very long process for big networks, and until the program finishes working, no windows are displayed so the user can be disoriented about this fact.
Maybe you could add a multithreaded behaviour so the main window is displayed with a progress bar before starting (and during) the network scan.
In my case, the program waits 2 minutes before showing something.
BTW, a very nice work Smile | :)
 
--
NinjaCross
www.ninjacross.com
AnswerRe: Now it works !memberH. S. Masud8 May '06 - 0:36 
Hi,
Thanks for the finding. The library scan for computers and servers in the network when it was first requested. After scaning the network it builds the collection of computers. I left the responsibility to the user of the class. I think it's not a good idea to build the collection in a different thread. Because by that time user may excess the collection. So, there will be a problem modifiying the collection in a different thread. May be a mutex will solve this problem. I'll try with different thread.
 
Thanks

 
Hasan Shahriar Masud
Senior Software Engineer
Kaz Software limited
Dhaka, Bangladesh
URL: http://www.hsmasud.com
E-Mail: info@hsmasud.com
JokeRe: Now it works !memberH. S. Masud8 May '06 - 21:52 
Hi,
I upgraded the application. It uses a different thread to load the list.
 
Thanks
 
Hasan Shahriar Masud
Senior Software Engineer
Kaz Software limited
Dhaka, Bangladesh
URL: http://www.hsmasud.com
E-Mail: info@hsmasud.com
GeneralRe: Now it works !memberNinjaCross9 May '06 - 0:13 
Absolutely much better Big Grin | :-D
Thanks again for you effort.
 
--
NinjaCross
www.ninjacross.com
 
-- modified at 6:14 Tuesday 9th May, 2006

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 21 Sep 2006
Article Copyright 2006 by H. S. Masud
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid