2010-05-18 25 views
12

我正在用C#編寫一個軟件,使用.NET 2檢測Windows計算機上是否存在活動的以太網連接。如何檢測Windows是否通過局域網或WiFi上的C#指令流量

重要的是它知道它是以太網而不是WiFi,因爲程序的行爲會有所不同,這取決於使用WebClient發送數據是通過WiFi還是以太網。

我已經嘗試使用System.Net.NetworkInformation.NetworkInterfaceType,但這似乎報告大量的WiFi卡'以太網'。

任何建議將不勝感激。

回答

5

根據該MSDN page有關NetworkInterface.NetworkInterfaceType屬性,

該屬性僅返回 一個子集在 NetworkInterfaceType枚舉中定義的可能的值。該 可能的值包括以下內容:

以太網FDDI環回了ppp滑令牌環未知

所以確定性,你可能會SOL。

但是,您可能能夠對可用網絡連接執行一些啓發式操作,以確定它們是WiFi還是有線。這些可能包括在很多迭代中發生的ping響應/延遲時間等。

此外,適配器的速度可能會用作提示。對於我的WiFi適配器,速度始終顯示爲「54000000」(例如54 mbs)。由於有一組常見的WiFi速度,這可能會有所幫助。

也許下面的代碼可以讓你開始:

using System; 
using System.Net.NetworkInformation; 
using System.Net; 

namespace ConsoleApplication7 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces(); 
      Ping pingObj = new Ping(); 

      for (int i = 0; i < adapters.Length; i++) 
      { 
       Console.WriteLine("Network adapter: {0}", adapters[i].Name); 
       Console.WriteLine(" Status:   {0}", adapters[i].OperationalStatus.ToString()); 
       Console.WriteLine(" Interface:   {0}", adapters[i].NetworkInterfaceType.ToString()); 
       Console.WriteLine(" Description:  {0}", adapters[i].Description); 
       Console.WriteLine(" ID:    {0}", adapters[i].Id); 
       Console.WriteLine(" Speed:    {0}", adapters[i].Speed); 
       Console.WriteLine(" SupportsMulticast: {0}", adapters[i].SupportsMulticast); 
       Console.WriteLine(" IsReceiveOnly:  {0}", adapters[i].IsReceiveOnly); 
       Console.WriteLine(" MAC:    {0}", adapters[i].GetPhysicalAddress().ToString()); 
       if (adapters[i].NetworkInterfaceType != NetworkInterfaceType.Loopback) 
       { 
        IPInterfaceProperties IPIP = adapters[i].GetIPProperties(); 
        if (IPIP != null) 
        { 
         // First ensure that a gateway is reachable: 
         bool bGateWayReachable = false; 
         foreach (GatewayIPAddressInformation gw in IPIP.GatewayAddresses) 
         { 
          Console.WriteLine(" Gateway:  {0} - ", gw.Address.ToString()); 

          // TODO: Do this many times to establish an average: 
          PingReply pr = pingObj.Send(gw.Address, 2000); 

          if (pr.Status == IPStatus.Success) 
          { 
           Console.WriteLine(" reachable ({0}ms)", pr.RoundtripTime); 
           bGateWayReachable = true; 
           break; 
          } 
          else 
           Console.WriteLine(" NOT reachable"); 
         } 
         // Next, see if any DNS server is available. These are most likely to be off-site and more highly available. 
         if (bGateWayReachable == true) 
         { 
          foreach (IPAddress ipDNS in IPIP.DnsAddresses) 
          { 
           Console.WriteLine(" DNS:   {0} - ", ipDNS.ToString()); 
           PingReply pr = pingObj.Send(ipDNS, 5000); // was 2000, increased for Cor in UK office 
           if (pr.Status == IPStatus.Success) 
           { 
            Console.WriteLine(" reachable ({0}ms)", pr.RoundtripTime); 
            Console.WriteLine(" --- SUCCESS ---"); 
            break; 
           } 
           else 
            Console.WriteLine(" NOT reachable"); 
          } 
         } 
        } // if (IPIP != null) 
       } 
      } // foreach (NetworkInterface n in adapters) 

      Console.ReadLine(); 
     } 
    } 
} 
相關問題