Logo
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
EPL Standings
 
 
Windows 7

Working in the Background : WORKING WITH THE NETWORK LIST MANAGER

4/21/2014 1:48:33 AM

In times past, developers often had to restore Windows Management Instrumentation (WMI) queries and direct Win32 API access using P/Invoke to discover details about the network. Yes, the .NET Framework does tell you a lot of useful information, such as the drive setup, but there are additional details, such as the network identifier and whether there's an Internet connection available, that can prove elusive to obtain. The Network List Manager makes it significantly easier to discover these details about your network. The Network List Manager example shows how to perform this task.

1. Configuring the Network List Manager Example

This example begins with a Windows Forms application. You'll need to add a Get Data button (btnGet) to obtain data about the current network connectivity and a list box (lstData) to output the results of the query. In addition, you'll need to add a reference to Microsoft.WindowsAPICodePack.DLL and provide the following using statement:

using Microsoft.WindowsAPICodePack.Net;

2. Writing the Network List Manager Code

The Network List Manager can provide a wealth of information about the network. It's easy to discover every connection and every network attached to a machine. Some of the information, such as the network identifier, is read-only. However, you can change other information, such as the network name. Listing 1 shows the statistics you can obtain about the connections and network attachments for a machine.

Example 1. Obtaining network connectivity information
private void btnGet_Click(object sender, EventArgs e)
{
// Remove any previous results.
lstData.Items.Clear();

// Obtain the current connected state.
if (!NetworkListManager.IsConnected)
{
// Display an error message.
MessageBox.Show("No network detected!");

// Exit the method.
return;
}
else
lstData.Items.Add("Network Connection Detected");

// Check for an Internet connection.
lstData.Items.Add("Internet Connection Available? " +
NetworkListManager.IsConnectedToInternet);

// Check the connectivity status.
lstData.Items.Add("Connectivity State: " +
NetworkListManager.Connectivity);


// Obtain a list of all of the network connections.
NetworkConnectionCollection Connections =
NetworkListManager.GetNetworkConnections();

// Check each network connection in turn.
foreach (NetworkConnection ThisConnection in Connections)
{
// Get the basic connection information.
lstData.Items.Add("Connection:");
lstData.Items.Add("\tAdapter ID: " +
ThisConnection.AdapterId);
lstData.Items.Add("\tConnection ID: " +
ThisConnection.ConnectionId);

// Check its connected state.
if (!ThisConnection.IsConnected)
{
// If the network isn't connected, exit.
lstData.Items.Add("\tNot Connected");
break;
}

// Display the connection information.
lstData.Items.Add("\tConnected");

// Display some connection specifics.
lstData.Items.Add("\tConnectivity: " +
ThisConnection.Connectivity);
lstData.Items.Add("\tDomain Type: " +
ThisConnection.DomainType);

// Get the network-specific information.
Network ThisNetwork = ThisConnection.Network;

// Display general network information.
lstData.Items.Add("Network:");
lstData.Items.Add("\tName: " +
ThisNetwork.Name);
lstData.Items.Add("\tNetwork ID: " +
ThisNetwork.NetworkId);

// Get specifics if the network is connected.
if (ThisNetwork.IsConnected)
{
lstData.Items.Add("\tConnected");
lstData.Items.Add("\tInternet Connection? " +
ThisNetwork.IsConnectedToInternet);
lstData.Items.Add("\tCategory: " +
ThisNetwork.Category);
lstData.Items.Add("\tConnected Time: " +
ThisNetwork.ConnectedTime);
lstData.Items.Add("\tConnectivity: " +
ThisNetwork.Connectivity);


lstData.Items.Add("\tCreated Time: " +
ThisNetwork.CreatedTime);
lstData.Items.Add("\tDescription: " +
ThisNetwork.Description);
lstData.Items.Add("\tDomain Type: " +
ThisNetwork.DomainType);
}
else
lstData.Items.Add("\tNot Connected");
}
}

The code begins by checking the connected state of the machine, using the NetworkListManager.IsConnected property. If the machine isn't connected, the code displays an error message and exits. When the code finds a connection, it displays the overall network information for the machine, such as the presence of an Internet connection.

A single machine can (and often does) have multiple connections. Each connection is associated with a physical network adapter. The user must enable the network adapter for it to show up with this check. The NetworkConnectionCollection object, Connections, contains all the connections for the machine. If the NetworkListManager.IsConnected property is true, there's always at least one NetworkConnection object to process.

The code relies on a foreach loop to process Connections as individual NetworkConnection objects, each identified as ThisConnection. As a minimum, each NetworkConnection has a valid AdapterId and ConnectionId property, so the code displays this information on-screen. The next step is to check ThisConnection.IsConnected to determine whether the NetworkConnection is actually connected to the network (it may be enabled but not connected to anything at all, or may be in an error state). If the code doesn't find a connection, it displays this fact and moves on to the next connection. The connection is associated with the lower levels of the network protocol, including the physical adapter.

Figure 1. The network information presented here can be useful in digging further into the network structure.

After the code displays the connection statistics, it moves on to the network statistics. The network is associated with the higher-level software settings. As shown in the example, you can check the network identifier and name even if the network isn't connected (which would likely indicate an error condition). The Network object, ThisNetwork, is what you use to check the amount of time that the user has remained connected and is also where you can get human-readable information about the network, including a description. Figure 1 shows typical output from this example.

Other -----------------
- Working in the Background : IMPLEMENTING APPLICATION RESTART AND RECOVERY
- Working in the Background : PROVIDING POWER MANAGEMENT (part 2) - Detecting a Change in Monitor State
- Working in the Background : PROVIDING POWER MANAGEMENT (part 1) - Getting the Power Management State
- Automating Windows 7 Installation : Customizing Images Using Deployment Image Servicing and Management (part 3) - Servicing the Operating System in an Image , Committing an Image
- Automating Windows 7 Installation : Customizing Images Using Deployment Image Servicing and Management (part 2) - Mounting an Image , Servicing Drivers in an Image
- Automating Windows 7 Installation : Customizing Images Using Deployment Image Servicing and Management (part 1) - Viewing Information about an Image with DISM
- Automating Windows 7 Installation : Applying an Image Using ImageX
- Automating Windows 7 Installation : Capturing an Image Using ImageX
- Microsoft Visio 2010 : Creating Web Pages from Visio Drawings (part 4) - Fine-tuning Web Pages and Battling Bugs - Saving a Visio Drawing as a Web Page
- Microsoft Visio 2010 : Creating Web Pages from Visio Drawings (part 3) - Fine-tuning Web Pages and Battling Bugs - Customizing Web Page Output
- Microsoft Visio 2010 : Creating Web Pages from Visio Drawings (part 2) - Exploring Visio-Generated Web Pages
- Microsoft Visio 2010 : Creating Web Pages from Visio Drawings (part 1) - Saving as Web Page
- Microsoft Visio 2010 : Sending Visio Files in Email, Saving as PDF or XPS Files
- Microsoft Visio 2010 : Introducing Data Graphics (part 2) - Creating Data Graphics,Applying Data Graphics to Shapes
- Microsoft Visio 2010 : Introducing Data Graphics (part 1) - What Is a Data Graphic?
- Microsoft Visio 2010 : Linking External Data to Shapes (part 6) - Using Link Data - Linking Data to Shapes Using Link Data
- Microsoft Visio 2010 : Linking External Data to Shapes (part 5) - Using Link Data - Preparing a Master for Link Data , Importing Data for Link Data
- Microsoft Visio 2010 : Linking External Data to Shapes (part 4) - Using the Database Wizard - Taking the Data-Linked Light Bulb Shape for a Spin
- Microsoft Visio 2010 : Linking External Data to Shapes (part 3) - Using the Database Wizard - Setting Up the Excel File as a Data Source
- Microsoft Visio 2010 : Linking External Data to Shapes (part 3) - Using the Database Wizard - Setting Up the Excel File as a Data Source
 
 
Most view of day
- Troubleshooting Hardware and Performance : Dealing with Error Messages, Performing a Clean Boot, Using the System Recovery Options
- Microsoft Exchange Server 2010 : Setting Up Transport Rules (part 3) - Selecting Actions
- Microsoft Exchange Server 2010 : Completing Transport Server Setup (part 3) - Enabling Anti-Spam Features
- Maintaining Windows 7 : Delete Unnecessary Files
- Developing Disk Images : Creating Image Builds
- SQL Server 2008 R2 : Overview of Resource Governor, Resource Governor Components
- Sharing Your Computer with Others : Display User Accounts
- Microsoft Exchange Server 2007 : Understanding the Client Access Server (part 1) - OWA
- Microsoft Word 2010 : Creating Desktop Publishing Documents - Adding Desktop Publishing Effects
- Editing Digital Video with Windows Live Movie Maker (part 5) - Editing Your Video - Using Titles
Top 10
- Windows Server 2012 : DHCP,IPv6 and IPAM - Exploring DHCP (part 3) - Creating IPv4 DHCP Scopes
- Windows Server 2012 : DHCP,IPv6 and IPAM - Exploring DHCP (part 2) - Installing DHCP Server and Server Tools
- Windows Server 2012 : DHCP,IPv6 and IPAM - Exploring DHCP (part 1)
- Windows Server 2012 : DHCP,IPv6 and IPAM - Understanding the Components of an Enterprise Network
- Microsoft OneNote 2010 : Using the Research and Translate Tools (part 3) - Translating Text with the Mini Translator
- Microsoft OneNote 2010 : Using the Research and Translate Tools (part 2) - Translating a Word or Phrase with the Research Pane
- Microsoft OneNote 2010 : Using the Research and Translate Tools (part 1) - Setting Options for the Research Task Pane, Searching with the Research Task Pane
- Microsoft OneNote 2010 : Doing Research with Linked Notes (part 2) - Ending a Linked Notes Session, Viewing Linked Notes
- Microsoft OneNote 2010 : Doing Research with Linked Notes (part 1) - Beginning a Linked Notes Session
- Microsoft OneNote 2010 : Doing Research with Side Notes (part 3) - Moving Side Notes to Your Existing Notes
 
 
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
2015 Camaro