2009-02-06 129 views

回答

135

您可以爲您在.NET 2.0的網絡連接使用GetIsNetworkAvailable()

System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable() 

要監控的IP地址或網絡可用性的變化而變化,從使用的NetworkChange類的事件:

System.Net.NetworkInformation.NetworkChange.NetworkAvailabilityChanged 
System.Net.NetworkInformation.NetworkChange.NetworkAddressChanged 
+8

這是否照顧「有限的可用性」? – tofutim 2011-09-13 00:51:57

+0

這並不總是在WPF中工作。一些筆記本電腦返回假,一些返回true – krilovich 2016-02-10 17:21:06

+0

這真的有用嗎?我只寫了這一行,並關閉了我的路由器,仍然返回true! – Mushfiq 2017-05-20 23:37:55

143

標記的答案是100%,但是,在某些情況下,標準方法被虛擬卡(虛擬盒,...)愚弄。通常希望根據其速度(串行端口,調制解調器等)丟棄一些網絡接口。

這裏是一段代碼,用於檢查這些情況:

/// <summary> 
    /// Indicates whether any network connection is available 
    /// Filter connections below a specified speed, as well as virtual network cards. 
    /// </summary> 
    /// <returns> 
    ///  <c>true</c> if a network connection is available; otherwise, <c>false</c>. 
    /// </returns> 
    public static bool IsNetworkAvailable() 
    { 
     return IsNetworkAvailable(0); 
    } 

    /// <summary> 
    /// Indicates whether any network connection is available. 
    /// Filter connections below a specified speed, as well as virtual network cards. 
    /// </summary> 
    /// <param name="minimumSpeed">The minimum speed required. Passing 0 will not filter connection using speed.</param> 
    /// <returns> 
    ///  <c>true</c> if a network connection is available; otherwise, <c>false</c>. 
    /// </returns> 
    public static bool IsNetworkAvailable(long minimumSpeed) 
    { 
     if (!NetworkInterface.GetIsNetworkAvailable()) 
      return false; 

     foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces()) 
     { 
      // discard because of standard reasons 
      if ((ni.OperationalStatus != OperationalStatus.Up) || 
       (ni.NetworkInterfaceType == NetworkInterfaceType.Loopback) || 
       (ni.NetworkInterfaceType == NetworkInterfaceType.Tunnel)) 
       continue; 

      // this allow to filter modems, serial, etc. 
      // I use 10000000 as a minimum speed for most cases 
      if (ni.Speed < minimumSpeed) 
       continue; 

      // discard virtual cards (virtual box, virtual pc, etc.) 
      if ((ni.Description.IndexOf("virtual", StringComparison.OrdinalIgnoreCase) >= 0) || 
       (ni.Name.IndexOf("virtual", StringComparison.OrdinalIgnoreCase) >= 0)) 
       continue; 

      // discard "Microsoft Loopback Adapter", it will not show as NetworkInterfaceType.Loopback but as Ethernet Card. 
      if (ni.Description.Equals("Microsoft Loopback Adapter", StringComparison.OrdinalIgnoreCase)) 
       continue; 

      return true; 
     } 
     return false; 
    } 
8

Microsoft Windows Vista和7使用NCSI(網絡連接狀態指示器)工藝:

  1. NCSI上執行DNS查找www.msftncsi.com,然後請求http://www.msftncsi.com/ncsi.txt。該文件是純文本文件,僅包含文本'Microsoft NCSI'。
  2. NCSI向dns.msftncsi.com發送DNS查找請求。該DNS地址應該解析爲131.107.255.255。如果地址不匹配,則認爲互聯網連接無法正常工作。
1

調用此方法檢查網絡連接。

public static bool IsConnectedToInternet() 
     { 
      bool returnValue = false; 
      try 
      { 

       int Desc; 
       returnValue = Utility.InternetGetConnectedState(out Desc, 0); 
      } 
      catch 
      { 
       returnValue = false; 
      } 
      return returnValue; 
     } 

把這下面的代碼行。

[DllImport("wininet.dll")] 
     public extern static bool InternetGetConnectedState(out int Description, int ReservedValue);