English 中文(简体)
专用网络界面IPv4 可用性——无连接性、地方性、互联网
原标题: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 )
        }

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

“Windows

最佳回答

我认为,上文所显示的Microsoft dialog 正在使用通过编码对网络定位意识预报软件进行编码所获得的信息。

问题回答

如以上评论所述,需要使用网络名单管理人,详情如下: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.

在你的解决方案探索者中,你可以正确点击“NETWORKLIST”的参考资料,你刚刚补充并选择了“在目标Browser”的“意见”,以检查你能够接触的界面。

enter image description here

从这里看,你可以执行下文所示的网络管理员班,以订阅连接变化活动。

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;

在收到连接变化活动后,你可以测试IsConnected ToInternet和IsConnected属性如下:

    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?





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签