【问题标题】:How to detect if Windows is directing traffic over LAN or over WiFi in C#如何在 C# 中检测 Windows 是通过 LAN 还是通过 WiFi 引导流量
【发布时间】:2011-02-20 23:57:11
【问题描述】:

我正在使用 .NET 2 用 C# 编写一个软件,它检测 Windows 机器上是否存在活动的 以太网 连接。

重要的是,它知道它是以太网而不是 WiFi,因为程序的行为会有所不同,具体取决于使用 WebClient 发送数据是通过 WiFi 还是以太网。

我尝试过使用 System.Net.NetworkInformation.NetworkInterfaceType 但这似乎报告了很多 WiFi 卡的“以太网”。

任何建议将不胜感激。

【问题讨论】:

    标签: c# .net windows networking


    【解决方案1】:

    根据这个MSDN page关于NetworkInterface.NetworkInterfaceType的属性,

    这个属性只返回一个子集 中定义的可能值 NetworkInterfaceType 枚举。这 可能的值包括:

    Ethernet Fddi Loopback Ppp Slip TokenRing Unknown

    所以确定性地你可能是 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();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-03
      • 2020-04-01
      • 2012-03-15
      • 2022-06-17
      相关资源
      最近更新 更多