5,550,131 members and growing! (19,107 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate

C# Server Enumerator

By Phil Bolduc

Server Enumerator in .NET style
C#Windows, .NET, .NET 1.1, NT4, Win2K, WinXPVS.NET2003, Visual Studio, Dev

Posted: 29 May 2002
Updated: 19 Nov 2003
Views: 103,150
Bookmarked: 36 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
18 votes for this Article.
Popularity: 5.26 Rating: 4.19 out of 5
0 votes, 0.0%
1
1 vote, 7.7%
2
0 votes, 0.0%
3
3 votes, 23.1%
4
9 votes, 69.2%
5

Introduction

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.

Notes

  • 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)
{
    Console.WriteLine(name);
}

//

// 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;
    Console.WriteLine(domainName);
}

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...

}

Conclusion

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

Changes

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.

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

Phil Bolduc



Occupation: Web Developer
Location: Canada Canada

Other popular C# 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 25 of 25 (Total in Forum: 25) (Refresh)FirstPrevNext
Subject  Author Date 
QuestionGetting 0 itemsmemberPaul_D1007:55 31 Oct '06  
GeneralEnumerate local pcs , workgroups etc...(PLEASE HELP)membergiddy_guitarist22:31 25 Aug '06  
GeneralUsing your DLL in my thesismemberssammut22:20 30 Mar '06  
GeneralNetwork Connectivity Issuememberjoaopereira@zmail.pt16:12 8 Jun '05  
GeneralRe: Network Connectivity Issuememberpwsmietan2:42 20 Sep '07  
GeneralA slightly different way...memberNakomis2:26 29 Jan '04  
GeneralWindow 2003 Active DirectorymemberMuhammed Yaseen1:27 26 Jan '04  
GeneralServer.GetServerType( string name ) has a bug.memberBen Houston9:43 30 Nov '03  
Generalthanks and how about continuemembercauchyer1:18 24 Nov '03  
GeneralThanks and Problemmembermaraymer15:45 28 Sep '03  
GeneralRe: Thanks and ProblemmemberPhil Bolduc22:13 30 Sep '03  
GeneralWindows 2003memberJASM11:45 3 Aug '03  
GeneralRe: Windows 2003membermaraymer15:38 28 Sep '03  
GeneralTHANKSmemberAndrei Matei6:50 10 Apr '03  
GeneralUnable to use Enumaration in VBmemberparth_410:22 1 Mar '03  
GeneralRe: Unable to use Enumaration in VBmembervbnetuk22:44 18 Mar '03  
GeneralRe: Unable to use Enumaration in VBmemberPhil Bolduc13:47 23 Mar '03  
GeneralRe: Unable to use Enumaration in VBmembervbnetuk21:49 23 Mar '03  
GeneralRe: Unable to use Enumaration in VBmemberPhil Bolduc22:40 23 Mar '03  
GeneralRe: Unable to use Enumaration in VBmemberJason Gaylord7:05 29 Apr '03  
GeneralRe: Unable to use Enumaration in VBsussAnonymous13:02 28 May '03  
GeneralRe: Unable to use Enumaration in VBmemberflindian7:50 19 Oct '03  
GeneralRe: Unable to use Enumaration in VBmemberPhil Bolduc22:47 21 Oct '03  
GeneralA small bugmemberRichard_D8:14 15 Aug '02  
GeneralVery Nice.memberRyanFarley6:33 23 Jul '02  

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

PermaLink | Privacy | Terms of Use
Last Updated: 19 Nov 2003
Editor: Nishant Sivakumar
Copyright 2002 by Phil Bolduc
Everything else Copyright © CodeProject, 1999-2008
Web10 | Advertise on the Code Project