Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specific network interface IPv4 availability - No connectivity, Local, Internet

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 )
        }
  • This question is not about general internet connectivity and as such using say GetIsNetworkAvailable() is not a solution
  • OperationalStatus.Up can be used to filter out some inactive network interfaces, but not all - OperationalStatus.Up leaves in some interfaces that show "No network access" for both IPv4 and IPv6
  • I'm also aware how to get the IPv4 UnicastAddresses, but then what / is that useful?
  • I could not find anything relevant in these sections of WMI

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

Windows 7 has this connectivy information

like image 392
Cel Avatar asked Jan 18 '23 08:01

Cel


1 Answers

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?

like image 194
Slion Avatar answered Jan 29 '23 10:01

Slion