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