Logo
programming4us
programming4us
programming4us
programming4us
Home
programming4us
XP
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone
 
programming4us
Windows 7

Working in the Background : WORKING WITH THE NETWORK LIST MANAGER

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
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
 
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
 
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server