Click here to Skip to main content
13,666,077 members
Click here to Skip to main content
Add your own
alternative version


51 bookmarked
Posted 29 May 2002

C# Server Enumerator

, 19 Nov 2003
Rate this:
Please Sign up or sign in to vote.
Server Enumerator in .NET style


Needing to list servers for a project I was working on, Marty Cerisano's article A C# Server Drop-down Control seemed perfect. Downloading and attempting to compile left me a little frustrated. The original code appears to be developed under a BETA version of Visual Studio .NET and would not compile under the released version. (MarshalAs.LPVoid was not defined, this has been updated in his May 30, 2002 release of his code).

I decided to rewrite the code and make it a bit more modular. I separated the code to enumerate the servers to it's own class. I removed the use of pointers and removed the use of the unsafe block.

The tricky part I found was removing the pointer use. Calculating the offset in the structure and using the Marshal.PtrToStructure() method to get correct data took some digging into the class library documentation.

There are three classes and an enumeration. They are:

  • ServerType - Enumeration listing possible server types, such as SQL Server or Terminal Server. ServerTypes can be logically OR'ed, i.e. the ServerType has the Flags attribute.
  • Servers - Collection of servers of specified type. Implements IEnumerable which allows foreach to be used.
  • ServerEnumerator - Enumerator to loop over the list of servers.
  • ServerComboBox - Example ComboBox control that uses the other classes.


  • The code will not allow you to get a list based on an AND conjunction. For example, you cannot ask for all SQL Servers that also are Terminal Servers. Attempting this, you will only get back a list of all server that are SQL Servers OR Terminal Servers. (It may be true that your SQL Server is also Terminal Server). You will have to manually query for each type and perform the intersection operation manually.
  • The AutoRefresh property will not automatically rescan the network. Setting AutoRefresh to true will cause any changes to the ServerType property to automatically call the Refresh() method. This is NOT an auto update setting.

Running the demo application

  1. Download both the source and the demo application.
  2. Extract each zip file to the same parent folder.
  3. Open the the source's solution and build.
  4. Open the demo's solution, build and run.

Using the Servers collection

using System;
using NetworkManagement;        

// List all the SQL Server database to the 
// console (using foreach)
Servers servers = new Servers( ServerType.SQLServer );
foreach (String name in servers)

// List all the Domains to the console.
Servers servers = new Servers( ServerType.DomainEnum );
IEnumerator i = servers.GetEnumerator()

while ( i.MoveNext() )
    string domainName = (string) i.Current;

Using the ServerComboBox

using System;
using NetworkManagement;        

public class ServerComboBoxForm : System.Windows.Forms.Form
    private ServerComboBox sCombo1;

    private void InitializeComponent()
        sCombo1 = new NetworkManagement.ServerComboBox();
        sCombo1.AutoRefresh = true; // turn force changes on
        sCombo1.ServerType = ServerType.DomainEnum;     
    // other code omitted...


This is a simple example of calling Windows API functions. Included is a sample application that demonstrates using the classes.


November 18, 2003

  • Fixed the ServerType enumeration to have CLR base type of long. Should fix problem VB.NET users were experiencing.
  • Remove restriction on using unsafe code by implementing Richard_D's suggestion. Now uses Marshal class instead of unsafe block.
  • Compiled binaries are for .NET 1.1. I do not have a .NET 1.0 system available to compile, sorry.
  • Samples are no longer bound to SourceSafe. Folder structure cleaned up.


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

Phil Bolduc
Web Developer
Canada Canada
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralThe IP Adress of the computers Pin
gabybosetti2-Feb-11 2:55
membergabybosetti2-Feb-11 2:55 
GeneralWow, thanks... Pin
Alfetta159_cp13-Jan-10 5:25
memberAlfetta159_cp13-Jan-10 5:25 
GeneralVery nice Pin
Roman_K18-Dec-09 14:47
memberRoman_K18-Dec-09 14:47 
QuestionGetting 0 items Pin
Paul_D10031-Oct-06 6:55
memberPaul_D10031-Oct-06 6:55 
GeneralEnumerate local pcs , workgroups etc...(PLEASE HELP) Pin
giddy_guitarist25-Aug-06 21:31
membergiddy_guitarist25-Aug-06 21:31 
GeneralUsing your DLL in my thesis Pin
ssammut30-Mar-06 21:20
memberssammut30-Mar-06 21:20 
GeneralNetwork Connectivity Issue Pin
joaopereira@zmail.pt8-Jun-05 15:12
memberjoaopereira@zmail.pt8-Jun-05 15:12 
GeneralRe: Network Connectivity Issue Pin
pwsmietan20-Sep-07 1:42
memberpwsmietan20-Sep-07 1:42 
GeneralA slightly different way... Pin
Nakomis29-Jan-04 1:26
memberNakomis29-Jan-04 1:26 
GeneralWindow 2003 Active Directory Pin
Muhammed Yaseen26-Jan-04 0:27
memberMuhammed Yaseen26-Jan-04 0:27 
GeneralServer.GetServerType( string name ) has a bug. Pin
Ben Houston30-Nov-03 8:43
memberBen Houston30-Nov-03 8:43 
Generalthanks and how about continue Pin
cauchyer24-Nov-03 0:18
membercauchyer24-Nov-03 0:18 
GeneralThanks and Problem Pin
maraymer28-Sep-03 14:45
membermaraymer28-Sep-03 14:45 
GeneralRe: Thanks and Problem Pin
Phil Bolduc30-Sep-03 21:13
memberPhil Bolduc30-Sep-03 21:13 
With respect to ServerComboBox, you will notice the AutoRefresh property only will refresh when you change either the ServerType or DomainName properties. It does not poll for changes. (I am sure you could modify it to poll?). Explicitly calling Refresh() on the ServerComboBox should return the current visible servers/machines as per the ServerType property. Calling Refresh() will ultamately call Servers.GetEnumerator() which in turn calls the Win32 API function NetServerEnum().

When reviewing my code with respect to your problem, I did notice that I did not check the return code from NetServerEnum(). Perhaps that is my error? When using the debugger, did you try to set a break point at Servers.GetEnumerator() to ensure it is being called? If it is and NetServerEnum() is being called, but your not getting the results you expect... I would think something is wrong with NetServerEnum(). (You did say you were running under Windows Server 2003?).

Other than that, I have no suggestions. If you do indeed find a bug in this project, please let me know so that I can fix.


GeneralWindows 2003 Pin
JASM3-Aug-03 10:45
memberJASM3-Aug-03 10:45 
GeneralRe: Windows 2003 Pin
maraymer28-Sep-03 14:38
membermaraymer28-Sep-03 14:38 
GeneralTHANKS Pin
Andrei Matei10-Apr-03 5:50
memberAndrei Matei10-Apr-03 5:50 
GeneralUnable to use Enumaration in VB Pin
parth_41-Mar-03 9:22
memberparth_41-Mar-03 9:22 
GeneralRe: Unable to use Enumaration in VB Pin
vbnetuk18-Mar-03 21:44
membervbnetuk18-Mar-03 21:44 
GeneralRe: Unable to use Enumaration in VB Pin
Phil Bolduc23-Mar-03 12:47
memberPhil Bolduc23-Mar-03 12:47 
GeneralRe: Unable to use Enumaration in VB Pin
vbnetuk23-Mar-03 20:49
membervbnetuk23-Mar-03 20:49 
GeneralRe: Unable to use Enumaration in VB Pin
Phil Bolduc23-Mar-03 21:40
memberPhil Bolduc23-Mar-03 21:40 
GeneralRe: Unable to use Enumaration in VB Pin
Jason Gaylord29-Apr-03 6:05
memberJason Gaylord29-Apr-03 6:05 
GeneralRe: Unable to use Enumaration in VB Pin
Anonymous28-May-03 12:02
sussAnonymous28-May-03 12:02 
GeneralRe: Unable to use Enumaration in VB Pin
flindian19-Oct-03 6:50
memberflindian19-Oct-03 6:50 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180810.1 | Last Updated 20 Nov 2003
Article Copyright 2002 by Phil Bolduc
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid