如何在 C# 中检测 Windows 是否通过 LAN 或 WiFi 引导流量

发布于 2024-09-01 20:30:37 字数 269 浏览 5 评论 0原文

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

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

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

任何建议将不胜感激。

I am writing a piece of software in C# using .NET 2 which detects whether there is an active ethernet connection on the Windows machine.

It is important that it knows that it is ethernet rather than WiFi as the program will behave differently depending on whether sending data with a WebClient is going over WiFi or Ethernet.

I have tried using System.Net.NetworkInformation.NetworkInterfaceType but this seems to report 'Ethernet' for a lot of WiFi cards.

Any suggestions will be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼眸里的那抹悲凉 2024-09-08 20:30:37

根据此 MSDN有关 NetworkInterface.NetworkInterfaceType 属性的页面

该属性仅返回一个子集
中定义的可能值
NetworkInterfaceType 枚举。这
可能的值包括以下内容:

以太网 Fddi 环回 Ppp Slip TokenRing 未知

因此,可以确定您可能是 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();
        }
    }
}

According to this MSDN page about the NetworkInterface.NetworkInterfaceType property,

This property only returns a subset of
the possible values defined in the
NetworkInterfaceType enumeration. The
possible values include the following:

Ethernet Fddi Loopback Ppp Slip TokenRing Unknown

So deterministically you may be SOL.

However, you may be able to perform some heuristics on the available network connections, to determine if they are WiFi or cable. These might include ping response/latency times taken over many iterations, etc.

Also, the speed of the adapter might be used as a hint. For my WiFi adapter the speed is always shown as "54000000" (e.g. 54 mbs). Since there is a set of common WiFi speeds, this could be helpful.

Perhaps the following code might get you started:

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();
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文