Поделиться через


Vista Network List Manager

Another new feature I came across is called "Network List Manager"!

This new COM Type Library allows you to enumerate all available networks or network interfaces and read or even write extended properties of those.

To use the new API, you need to add a COM reference to the "Network List Manager 1.0 Type Library" to your project. 

After doing that, you can instantiate a NetworkListManagerClass as an entry to the NLM API.

 NETWORKLIST.NetworkListManagerClass _networkListManager = new NetworkListManagerClass();

On the newly created object, there are a bunch of events, which get fired if the network connectivity or any other properties of a network change, as well as a lot of methods..

To understand what they do you need to differentiate networks and network-connections:

  • Network Connections (INetworkConnection) represent unique connections to a network. There can be multiple connections to a single network.
  • Every computer can be connected to several Networks (INetwork) over multiple Network Connections.

To build a simple online/offline sensing application you can subscribe the ConnectivityChanged event, which monitors the overall connectivity property of ALL networks. But you could also be notified if a single network connectivity changes..

 _networkListManager.ConnectivityChanged += new 
    INetworkListManagerEvents_ConnectivityChangedEventHandler(ConnectivityChanged);

After attaching the event handler you can access the NLM_CONNECTIVITY enumeration value of the delegate to determine the computer's new connectivity:
 
Another possible way is querying the NetworkListManager's properties:

  bool connected = _networkListManager.IsConnected;
 bool internet = _networkListManager.IsConnectedToInternet;

If you want to find out more information about a network or a network connection use on of the two ways to iterate through them:

 foreach(INetworkConnection c in 
   _networkListManager.GetNetworkConnections())
{
}

or

 foreach (INetwork n in _networkListManager.GetNetworks(
   NLM_ENUM_NETWORK.NLM_ENUM_NETWORK_ALL))
{
}

where you can chose the type of network (connected, disconnected, all) with the NLM_ENUM_NETWORK enumeration.

After having queried for a specific network (directly or via the connection) you can access several properties like the name, description, category (vista categories: public, private, work/domain), connectivity (none, IPv4, IPv6, Intranet, Internet, ...) or even set the name and description of it.

Caution: You cannot use the NetworkListManagerClass over different threads, but only out of the thread, which instantiated it!

If you like to, you can download an attached sample below!

NLM_Demo.zip

Comments

  • Anonymous
    May 30, 2007
    PingBack from http://www.entwicklerblog.com/?p=56

  • Anonymous
    February 03, 2008
    knom, thank you very much for this great article! It helped me a lot. But I have a question. If I now name of my Internet connection (for example, "Domolink"). How to establish this connection from my program? I use Windows Vista. PS. I apologize for my English :^)