C#判断网卡是否可用
2018-07-20 来源:open-open
微软提供的函数不能再多网卡的情况下,判断指定网卡的网线连接状态。以下代码刚好能够解决这个问题。
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; } } } }
标签: 代码
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
最新资讯
热门推荐