September 29, 2008

MCTS .NET Certification Station

How Do You Retrieve Information About All Network Connections?

One of the objectives of the MCTS .NET Framework exam is to Embed management
information and events into a .NET Framework application. (Refer
System.Management namespace) and within that exam objective there is the
specific objective



Retrieve information about all network connections.

I started to look through the System.Management namespace and found the
ManagementObject and the ManagementObjectSearcher. Then the clarity of the
examples of the descriptions and examples began to muddy. I understood that the
ManagementObjectSearcher was using an ObjectQuery object to perform the search.
It was completely unclear WHAT was being queried.



My investigations then lead me to the Windows Management Interface, known as WMI.
What does this have to do with ObjectQuery and the ManagementObjectSearcher
classes? Well, WMI and the underlying WMI classes are what is actually being
queried. The queries are written in a SQL derivative named WQL. There is a
dizzying variety of information exposed by WMI.



After the mechanics of writing a query are understood, the next task is to find
the correct WMI class that encapsulates the information you need. In the case of
our question at hand, we are looking for networking information.



At first I was happy to find the Win32_NetworkConnection class, but found that
it did not return information for me. (Despite having a connection up for
writing this post). The next promising class was the Win32_NetworkAdapter class.
Using this class I created the example below that retrieves information about
all network connections.



A "gotcha" to consider before reviewing the code below:

My first version of the code listed a number of adapters. I then determined that
it listed physical AND logical network adapters. You will see an if block in the
code to distinguish between physical and logical adapters.



Retrieve Information About All Network Connections Example using System;

// A reference to the System.Management assembly needs

// to be added to your project...

using System.Management;



namespace NetworkConnectionsExample

{

class Program

{

static void Main(string[] args)

{

// In order to use ManagementObjectSearcher, we need to

// supply an query string to search on. We are searching

// against differenct classes exposed by the Windows Management

// Interface(WMI).

ManagementObjectSearcher network_connections =

new ManagementObjectSearcher(

"SELECT * FROM Win32_NetworkAdapter");



Console.WriteLine("\nFirst we will just list the names of all physical
adapters");

Console.WriteLine("and their connection status");

Console.WriteLine("-----------------------------");



// The query of the underlying ObjectQuery object is not executed

// until the Get() function is executed.

foreach (ManagementObject connection in network_connections.Get())

{

// Knowledge by experiment... on my machine there were a number

// of network adapters displayed on my first version of this code.

// I know I only have three pyhisical adapters (Wireless, 10/100,

// and 1394) so I empirically determined that the Management Objects

// with the NetConnectionStatus property were physical apdapters and

// not logical adapters... mileage may vary, results not insured ;)

if (connection.Properties["NetConnectionStatus"].Value != null)

{

Console.WriteLine("Product Name: " + connection.Properties["ProductName"].Value);

Console.WriteLine("Connection Status: " +
connection.Properties["NetConnectionStatus"].Value);

Console.WriteLine("-----------------------------");

}

}



Console.WriteLine("\nNext we will show all of the information availalbe for each
physical adapter.");

Console.WriteLine("Probably more than you ever wanted to know about your network
adapter, but your software might need to know it...");

Console.WriteLine("-----------------------------");



foreach (ManagementObject connection in network_connections.Get())

{

if (connection.Properties["NetConnectionStatus"].Value != null)

{

foreach (PropertyData d in connection.Properties)

{

Console.WriteLine("-- Name: " + d.Name + " Value: " + d.Value);

}

Console.WriteLine("-----------------------------");

}

}

}

}

}





Additional Resources

click here for more info mcitp training

click here for more info mcst training

click here for more info mcse training

click here for more info mcse 2003 training

click here for more info mcdst training

No comments:

Bookmark and Share