4

How to identify connectivity status of a specific NetworkInterface ?

        NetworkInterface[] nets = NetworkInterface.GetAllNetworkInterfaces();

        foreach (var n in nets)
        {
            // TODO: determine connectivity status of each network interface
            // ( mainly interested in IPv4 connectivity )
        }

i.e. extracting per interface status as Internet, Local, Limited or None

Windows 7 has this connectivy information

Community
  • 1
  • 1
Cel
  • 6,467
  • 8
  • 75
  • 110

2 Answers2

6

As mentioned in a comment above you need to use Network List Manager as explained there

To do so first add a reference to it as shown in the the screenshot below. Right click on your project in your Visual Studio solution. Select Add > Reference... Go to COM and find the "Network List Manager 1.0 Type Library" entry using the search box.

Add Network List Manager Reference To Your Project

That will generate an Interop DLL to this COM interface in your binary output folder. That DLL is named Interop.NETWORKLIST.dll.

In your Solution Explorer you can right click on the NETWORKLIST reference you just added and select "View in Object Browser" to inspect the interfaces you get access to.

enter image description here

From here you can implement a Network Manager class as shown below to subscribe to connectivity change events.

using System;
using System.Runtime.InteropServices.ComTypes;
using System.Diagnostics;
using NETWORKLIST;

namespace SharpDisplayManager
{
    public class NetworkManager: INetworkListManagerEvents, IDisposable
    {
        public delegate void OnConnectivityChangedDelegate(NetworkManager aNetworkManager, NLM_CONNECTIVITY aConnectivity);
        public event OnConnectivityChangedDelegate OnConnectivityChanged;

        private int iCookie = 0;
        private IConnectionPoint iConnectionPoint;
        private INetworkListManager iNetworkListManager;


        public NetworkManager()
        {
            iNetworkListManager = new NetworkListManager();
            ConnectToNetworkListManagerEvents();
        }

        public void Dispose()
        {
            //Not sure why this is not working form here
            //Possibly because something is doing automatically before we get there
            //DisconnectFromNetworkListManagerEvents();
        }


        public INetworkListManager NetworkListManager
        {
            get { return iNetworkListManager; }
        }

        public void ConnectivityChanged(NLM_CONNECTIVITY newConnectivity)
        {
            //Fire our event
            OnConnectivityChanged(this, newConnectivity);
        }

        public void ConnectToNetworkListManagerEvents()
        {
            Debug.WriteLine("Subscribing to INetworkListManagerEvents");
            IConnectionPointContainer icpc = (IConnectionPointContainer)iNetworkListManager;
            //similar event subscription can be used for INetworkEvents and INetworkConnectionEvents
            Guid tempGuid = typeof(INetworkListManagerEvents).GUID;
            icpc.FindConnectionPoint(ref tempGuid, out iConnectionPoint);
            iConnectionPoint.Advise(this, out iCookie);

        }

        public void DisconnectFromNetworkListManagerEvents()
        {
            Debug.WriteLine("Un-subscribing to INetworkListManagerEvents");
            iConnectionPoint.Unadvise(iCookie);
        } 
    }
}

You can instantiate your Network Manager like this:

iNetworkManager = new NetworkManager();
iNetworkManager.OnConnectivityChanged += OnConnectivityChanged;

Upon receiving connectivity change events you could test IsConnectedToInternet and IsConnected attribute as shown below:

    public void OnConnectivityChanged(NetworkManager aNetwork, NLM_CONNECTIVITY newConnectivity)
    {
        //Update network status
        UpdateNetworkStatus();          
    }

    /// <summary>
    /// Update our Network Status
    /// </summary>
    private void UpdateNetworkStatus()
    {
        //TODO: Test the following functions to get network and Internet status
        //iNetworkManager.NetworkListManager.IsConnectedToInternet
        //iNetworkManager.NetworkListManager.IsConnected
    }

Here is a related question: INetworkConnectionEvents Supports what?

Community
  • 1
  • 1
Slion
  • 2,558
  • 2
  • 23
  • 27
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – David says Reinstate Monica Feb 09 '15 at 18:10
  • Here is a more extensive answer. I hope my answer does not get deleted this time around :) – Slion May 26 '15 at 13:56
3

I think the Microsoft dialog you show above is using information gained by coding against the Network Location Awareness API.

http://msdn.microsoft.com/en-us/library/ee264321%28v=VS.85%29.aspx

rushman
  • 532
  • 2
  • 8
  • 1
    Thanks! Found a [C# tutorial here](http://www.codeproject.com/KB/IP/usenetworklist.aspx), although [this library seems to be the preferred way to access the functionality](http://archive.msdn.microsoft.com/WindowsAPICodePack). Note 1) even though the latter library is abandonware now, it is still probably the most convenient managed way to access NLM 2) NLM only works with Vista and Windows 7 :( – Cel Nov 06 '11 at 16:35
  • 1
    Yes, you have to use the old NLA library for XP and below, although it doesn't offer the same level of funtionality as NLM. – rushman Nov 06 '11 at 17:31
  • The broken link to the WindowsAPICodePack in the previous comment can now be found via Nuget with `Install-Package WindowsAPICodePack-Core` or at https://www.nuget.org/packages/WindowsAPICodePack-Core/ – Katstevens Feb 22 '16 at 05:31