Click here to Skip to main content
Licence 
First Posted 9 Jan 2007
Views 55,281
Bookmarked 52 times

Build Simple Network Browser

By | 9 Jan 2007 | Article
Build Simple Network Browser to Enumerate Computers and Users on the Local Network

Introduction

While working on SQL Server management tool for mobile devices - Siccolo, I created a simple applet to be able to browse computers on the local network within a domain and retrieve additional information (such as list of users, services).
See more at Articles from Siccolo!

The code presented allows to build simple network browser

Using the code

.NET allows to retrieve almost any information about local network by using WMI - Windows Management Instrumentation - service with ManagementObjectSearcher class and in conjunction with Active Directory Service Interfaces (ADSI) - Active Directory hierarchy.

The code is broken into several methods:

  1. Get Domain Name location computer belongs to:

        // VB.NET //
        ...
       Public Function GetLocalComputerInfo() As Boolean
    
            Dim query As <CODE>ManagementObjectSearcher</CODE>
            Dim queryCollection As <CODE>ManagementObjectCollection</CODE>
    
            Dim query_command As String = "SELECT * FROM Win32_ComputerSystem"
    
            Dim msc As ManagementScope = New ManagementScope("root\cimv2")
    
            Dim select_query As SelectQuery = New SelectQuery(query_command)
    
            query = New ManagementObjectSearcher(msc, select_query)
            queryCollection = query.Get()
    
            Dim management_object As <CODE>ManagementObject</CODE>
    
            For Each management_object In queryCollection
                m_local_domain_name = management_object("Domain")
                m_local_computer_name = management_object("Name")
            Next management_object
    
            Return True
        End Function
        ...
    
    where:
    msc As ManagementScope sets a scope for management operations - defines the WMI namespace in which management operations are performed.

    Once domain name is known, we are able to retrieve list of computers within that domain.

  2. Enumerate computers within a domain:

        // VB.NET //
        ...
        Public Function GetComputersInfoCollection(ByVal domain As String) _<BR>                                                        As DirectoryEntry
            Dim domainEntry As DirectoryEntry = New DirectoryEntry("WinNT://" _<BR>                                                                + domain)
            domainEntry.Children.SchemaFilter.Add("computer")
            Return domainEntry
        End Function
        ...
    
    where:
    method returns instance of DirectoryEntry - node in in the Active Directory hierarchy. that corresponds to the given domain.

    Next, let's retrieve list of users that belong to that domain.

  3. Enumerate users within a domain:

        // VB.NET //
        ...
        Public Function GetUsersInfoCollection(ByVal domain As String) _ <BR>                                           As ManagementObjectCollection
            Dim query As ManagementObjectSearcher
            Dim queryCollection As ManagementObjectCollection
    
            Dim msc As ManagementScope = New ManagementScope("root\cimv2")
            Dim query_command As String = _
            "SELECT * FROM Win32_UserAccount  WHERE Domain=" & _
                Chr(34).ToString() & domain & Chr(34).ToString()
            'Win32_UserAccount:
            'see http://msdn.microsoft.com/library/default.asp?<BR>        'url=/library/en-us/wmisdk/wmi/win32_useraccount.asp
            'class Win32_UserAccount : Win32_Account
            '{
            '  uint32 AccountType;
            '  string Caption;
            '  string Description;
            '  boolean Disabled;
            '  string Domain;
            '  string FullName;
            '  datetime InstallDate;
            '  boolean LocalAccount;
            '  boolean Lockout;
            '  string Name;
            '  boolean PasswordChangeable;
            '  boolean PasswordExpires;
            '  boolean PasswordRequired;
            '  string SID;
            '  uint8 SIDType;
            '  string Status;
            '};
            '
            Dim select_query As SelectQuery = New SelectQuery(query_command)
    
            query = New ManagementObjectSearcher(msc, select_query)
            queryCollection = query.Get()
    
            Return queryCollection
        End Function
        ...
    


    In addition to be able to retrieve above information, we can also obtain services collection for a given machine:

  4. Enumerate services on a computer using WMI:


        // VB.NET //
        ...
        Public Function GetServicesInfoCollection(ByVal computer_name _<BR>                                                       As String) _<BR>                      As ManagementObjectCollection
            Dim query As ManagementObjectSearcher
            Dim queryCollection As ManagementObjectCollection
    
            Dim msc As ManagementScope = New ManagementScope("\\" &_<BR>                                      computer_name & "\root\cimv2")
            Dim query_command As String = "SELECT * FROM Win32_Service"
            'Win32_UserAccount:
            'see http://msdn.microsoft.com/library/en-us/wmisdk/wmi/<BR>        'win32_service.asp?frame=true<BR>        '
            '        class Win32_Service : Win32_BaseService
            '{
            '  boolean AcceptPause;
            '  boolean AcceptStop;
            '  string Caption;
            '  uint32 CheckPoint;
            '  string CreationClassName;
            '  string Description;
            '  boolean DesktopInteract;
            '  string DisplayName;
            '  string ErrorControl;
            '  uint32 ExitCode;
            '  datetime InstallDate;
            '  string Name;
            '  string PathName;
            '  uint32 ProcessId;
            '  uint32 ServiceSpecificExitCode;
            '  string ServiceType;
            '  boolean Started;
            '  string StartMode;
            '  string StartName;
            '  string State;
            '  string Status;
            '  string SystemCreationClassName;
            '  string SystemName;
            '  uint32 TagId;
            '  uint32 WaitHint;
            '};
    
            Dim select_query As SelectQuery = New SelectQuery(query_command)
    
            query = New ManagementObjectSearcher(msc, select_query)
            queryCollection = query.Get()
    
            Return queryCollection
        End Function  
        ...
    

    Points of Interest

    If you would like to read more - please take a look at Siccolo - Free Mobile Management Tool For SQL Server and more articles at Development Articles from Siccolo!

    History

    no improvements so far. nearly perfect.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

aleksisa



United States United States

Member



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. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 PinmemberMember 307350021:17 21 Feb '11  
GeneralMy vote of 1 PinmemberOnAClearDiskYouCanSeekForever15:53 22 Apr '10  
Generalaccess denied Pinmembercemalpcstore3:28 27 Feb '07  
GeneralRe: access denied Pinmemberaleksisa16:18 27 Feb '07  
QuestionHow to retrive Shared Files(not folder) using WMI Pinmemberi_razi9:01 23 Feb '07  
I had searched a lot but unable to get help on reading shared Open Files using WMI. Please help on this. I can retrive till folder using this sample code. But this one is not working for Shared Files. I would like to read Computer Managemnt-Shared Folder-Open Files Frown | :-(
 
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT *
FROM Win32_ConnectionShare");
foreach (ManagementObject connectionShare in searcher.Get())
{
// Win32_Share
string antecedent = connectionShare["Antecedent"].ToString();
Console.WriteLine("Antecedent: " + antecedent);
ManagementObject share = new ManagementObject(antecedent);
 
// Win32_ServerConnection
string dependant = connectionShare["Dependent"].ToString();
Console.WriteLine("Dependant: " + dependant);
ManagementObject connection = new ManagementObject(dependant);
Console.WriteLine(share["Name"].ToString());
 
if (connection != null && connection["Name"] != null)
Console.WriteLine(connection["Name"].ToString());
 
Console.WriteLine("\n");
}
 


 
adfg

AnswerRe: How to retrive Shared Files(not folder) using WMI Pinmemberaleksisa6:21 26 Feb '07  
GeneralNew key word missing form variable definitions PinmemberToothRobber8:50 16 Jan '07  
GeneralErrors and No Source Code Pinmemberhswear318:45 15 Jan '07  
GeneralRe: Errors and No Source Code Pinmemberaleksisa4:39 16 Jan '07  
GeneralManagementObject PinmemberJaeman15:26 15 Jan '07  
GeneralRe: ManagementObject Pinmemberaleksisa4:36 16 Jan '07  
GeneralRe: ManagementObject PinmemberJaeman18:08 16 Jan '07  
GeneralWhen running your application NullReferenceException PinmemberToothRobber7:49 15 Jan '07  
QuestionSource Code for your application? PinmemberToothRobber7:48 15 Jan '07  
AnswerRe: Source Code for your application? Pinmemberaleksisa4:41 16 Jan '07  
GeneralRe: Source Code for your application? PinmemberCole Knight8:37 18 Mar '11  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 9 Jan 2007
Article Copyright 2007 by aleksisa
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid