Specific Network Interface IPv4 Availability - No Connectivity, Local, Internet

How to determine the connection 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. retrieval for each interface status as Internet, local, limited or not

Windows 7 has this connectivy information

+4
source share
2 answers

I think that in the Microsoft dialog box that you show above, the information obtained by encoding using the network location API is used.

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

+3
source

As mentioned above, you need to use the Network List Manager as described there

To do this, first add a link to it, as shown in the screenshot below. Right-click on your project in your Visual Studio solution. Choose Add> Link .... Go to "COM" and find the entry "Network List 1.0 Library" using the search field.

Add Network List Manager Reference To Your Project

This will create an Interop DLL for this COM interface in your binary output folder. This DLL is called Interop.NETWORKLIST.dll.

In your Solution Explorer, you can right-click on the NETWORKLIST link you just added and select โ€œView in Object Explorerโ€ to check the interfaces available to you.

enter image description here

Here you can implement the Network Manager class, as shown below, to subscribe to connection 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 create your network manager as follows:

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

After receiving connection change events, you can check the IsConnectedToInternet and IsConnected attributes, 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?

+4
source

Source: https://habr.com/ru/post/1379871/


All Articles