• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

判断网卡是否可用

C# 水墨上仙 2877次浏览

微软提供的函数不能再多网卡的情况下,判断指定网卡的网线连接状态。只能自己写一个比较傻的了。


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.NetworkInformation;
using System.Threading;

namespace NetworkAdapterTestEvent
{
    class Program
    {
        static void Main(string[] args)
        {
            NetworkAdapterAvailable newtest = new NetworkAdapterAvailable();

            newtest.Subscribe("本地连接",new NetworkAdapterAvailable.NetworkAdapterChangedEventHandler(NetowrkAdapter_changed));
            newtest.StartMonitor();

            Console.ReadLine();
            newtest.StopMonitor();
            newtest.Unsubscribe("本地连接", new NetworkAdapterAvailable.NetworkAdapterChangedEventHandler(NetowrkAdapter_changed));
        }

        static void NetowrkAdapter_changed(object sender, NetworkAdapterChangedArgument e)
        {
            Console.WriteLine("Name:{0}\tStatus:{1}", e.AdapterName, e.AdapterStatus.ToString());
        }
    }

    public class NetworkAdapterAvailable
    {
        public delegate void NetworkAdapterChangedEventHandler(object send , NetworkAdapterChangedArgument e);
        
        public event NetworkAdapterChangedEventHandler NetworkAdapterChanged;

        private Thread _AdapterCheckThread;
        private string _AdapterName;
        private bool _Stop = false;
        private NetworkInterface[] _adapters;

        public NetworkAdapterAvailable()
        {
            _adapters = NetworkInterface.GetAllNetworkInterfaces();
        }

        /// <summary>
        /// 订阅
        /// </summary>
        /// <param name="_adaptername_"></param>
        /// <param name="_EventHandler_"></param>
        public void Subscribe(string _adaptername_ , NetworkAdapterChangedEventHandler _EventHandler_)
        {
            _AdapterName = _adaptername_;

            if (_EventHandler_ != null)
            {
                NetworkAdapterChanged += _EventHandler_;
            }
        }

        /// <summary>
        /// 取消订阅
        /// </summary>
        /// <param name="_adaptername_"></param>
        /// <param name="_EventHandler_"></param>
        public void Unsubscribe(string _adaptername_, NetworkAdapterChangedEventHandler _EventHandler_)
        {
            if (_EventHandler_ != null)
            {
                NetworkAdapterChanged -= _EventHandler_;
            }
        }

        /// <summary>
        /// 开始监控
        /// </summary>
        public void StartMonitor()
        {
            _AdapterCheckThread = new Thread(new ThreadStart(AdapterCheckHandler));
            _AdapterCheckThread.IsBackground = true;
            _AdapterCheckThread.Start();
        }

        public void StopMonitor()
        {
            _Stop = true;

            if (_AdapterCheckThread != null)
            {
                _AdapterCheckThread.Join();
            }

        }

        private void AdapterCheckHandler()
        {
            while (!_Stop)
            {
                NetworkInterface[] __adapters = NetworkInterface.GetAllNetworkInterfaces();

                for (int i = 0; i < __adapters.Length; i++)
                {
                    if (_adapters[i].Name == _AdapterName)
                    {
                        if (_adapters[i].OperationalStatus != __adapters[i].OperationalStatus)
                        {
                            NetworkAdapterChanged(this, new NetworkAdapterChangedArgument(_AdapterName, __adapters[i].OperationalStatus));
                            _adapters[i] = __adapters[i];
                        }
                    }
                }

                Thread.Sleep(100);
            }
        }
    }

    public class NetworkAdapterChangedArgument : EventArgs
    {
        private string _AdapterName;
        private OperationalStatus _AdapterStatus;

        public NetworkAdapterChangedArgument(string _adaptername_, OperationalStatus _adapterstatus_)
        {
            _AdapterName = _adaptername_;
            _AdapterStatus = _adapterstatus_;
        }

        public string AdapterName
        {
            get { return _AdapterName; }
        }

        public OperationalStatus AdapterStatus
        {
            get { return _AdapterStatus; }
        }
    }
}


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明判断网卡是否可用
喜欢 (0)
加载中……