Checking network status in C#

后端 未结 7 1948
时光取名叫无心
时光取名叫无心 2020-11-30 05:05

How do I check that I have an open network connection and can contact a specific ip address in c#? I have seen example in VB.Net but they all use the \'My\' structure. Than

7条回答
  •  一个人的身影
    2020-11-30 05:50

    If you want to monitor for changes in the status, use the System.Net.NetworkInformation.NetworkChange.NetworkAvailabilityChanged event:

    NetworkChange.NetworkAvailabilityChanged 
        += new NetworkAvailabilityChangedEventHandler(NetworkChange_NetworkAvailabilityChanged);
    _isNetworkOnline = NetworkInterface.GetIsNetworkAvailable();
    
    
    // ...
    void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
    {
        _isNetworkOnline  = e.IsAvailable;
    }
    

提交回复
热议问题