65.9K
CodeProject is changing. Read more.
Home

List Peer Servers and their IP Addresses inside a Workgroup.

Aug 26, 2002

3 min read

viewsIcon

64312

downloadIcon

382

How to list the peer servers and IP addresses in the workgroup - A simple approach.

Sample Image - PeerLister.jpg

Introduction

How can peer servers inside a work group be listed. I wanted to demonstrate a simple method to perform this operation. System.Remoting namespace provides certain APIs using which this can be performed. At CodeProject, I saw a couple of articles performing similar operations in MC++ and they printout the IP addresses too.

The articles are available at these links:

Overview

I chose to use the Shell basics. Using Shell basics, several tasks can be done very simply. The Shell32 DLL is interoped with the project and it exposes a number of methods, classes and interfaces to perform a lot of operations, like using the Windows UI.

The Shell organizes these objects into a hierarchical structure called the namespace, which provides users and applications with a consistent and efficient way to access and manage objects. Users interact with the namespace through the Shell's graphical UI or through an application. Applications interact with the namespace through the Shell's application programming interface (API). This section is an introduction to the Shell API.

There are plenty of objects used to create applications and manage the operating system. The most familiar of these objects are the folders and files that reside on computer disk drives and over networks.

Some of the basics ,how-tos and the APIs used will be explained in this article.

The platform considered is Windows 2000 Server and such a server present under a workgroup server. The requirement is to view the peer servers under the workgroup. Shell basics are used to perform the above said task.

Some Preliminaries

The workgroup when opened using an Explorer, displays the servers that are present in it. One has to understand the differences between:

  1. Folder
  2. NameSpace
  3. FolderItem
  4. Link, etc.

An object of type Shell32.Shell is created and another object of type Shell32.Folder is also created. There are some special folders specified. Such as ShellSpecialFolderConstants.ssfNETWORK represents the Network Places folder. My Documents, Control Panel and several special folders are defined and are identified by separate constants.

The folder is parsed only when isFileSystem return false. The link is opened by using Shell.NameSpace(object) and iterated until the workgroup server folder is reached.

Once the namespace is known, the items() gives a collection, from which an individual item can be retrieved. The code is given below:

Shell32.Shell sh = new Shell32.Shell();
Shell32.Folder currFolder = 
  sh.NameSpace(Shell32.ShellSpecialFolderConstants.ssfNETWORK);
int num = 
  sh.NameSpace(Shell32.ShellSpecialFolderConstants.ssfNETWORK).Items().Count;
MessageBox.Show("The Num of Items in special folder in the local",
  currFolder.Items().Count.ToString());
					
for(int i=0;i<num;i++)
{

 if(currFolder.Items().Item(i).IsFileSystem==false)
 {
  if(currFolder.Items().Item(i).Type.ToString()=="")
  {
   MessageBox.Show(currFolder.Items().Item(i).Name);
   int no =sh.NameSpace(currFolder.Items().Item(i)).Items().Count;
   currFolder = sh.NameSpace(currFolder.Items().Item(i));
   MessageBox.Show(no.ToString());

   MessageBox.Show(currFolder.Items().Item(0).Name+": Cool");
   currFolder=
    sh.NameSpace(sh.NameSpace(currFolder.Items().Item(0)).Items().Item(0));
   MessageBox.Show(currFolder.Items().Count+": Count");

   MessageBox.Show(mycount.ToString());
						
   for(int k=1;k<mycount;k++)
   {
    try
    {

     listView1.Items.Add(new ListViewItem
      (currFolder.Items().Item(k).Name+k.ToString()));

    }
    catch(Exception myexcep)
    {
								
     MessageBox.Show(myexcep.ToString()+" : Error");

    }
   }
  }
 }
}

Update

There is a very simple method to find the IP address of the servers under the workgroup server. These are found in the System.Net namespace. The peer servers are added in to a ListView control as listviewItem. The Net Namespace contains a plethora of classes and APIs for network access. The IP address can be found by passing the domain name of the server.

Sample Image - IP Address.jpg

The giveAddress method returns the IP address of the Server.

this.listView1.DoubleClick += new System.EventHandler(this.giveAddress);

The following is an example of how it is done....

private void giveAddress(object sender, System.EventArgs e)
{
 MessageBox.Show(System.Net.Dns.Resolve(listView1.FocusedItem.Text).
 AddressList.GetValue(0).ToString());
}

The MessageBox displays the IP address of the selected server.

Conclusion

This is an illustration of the use of the Shell basics and a method for how the peers in a workgroup are listed and the IP addresses of the servers found.