如何检测Windows是否通过LAN或WiFi通过C#
我在C#中使用.NET 2编写一个软件,它检测
Windows机器上是否有活动的以太网连接.
重要的是它知道它是以太网而不是WiFi,因为程序的行为会有所不同,这取决于是否通过WiFi或以太网发送WebClient数据. 我已经尝试使用System.Net.NetworkInformation.NetworkInterfaceType,但这似乎报告’以太网’为很多WiFi卡. 任何建议将不胜感激.
根据这个
MSDN page关于NetworkInterface.NetworkInterfaceType的属性,
所以确定性地你可能是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(); } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- Windows10纯净原版系统安装方法
- xaml – x中ElementName的替代:与DataTemplates绑定
- Win10禁用驱动签名的命令
- Windows server 2012 R2下安装sharepoint2013
- winrt-xaml – ListPickerFlyout忽略Windows Phone 8.1中父
- 终端 – 如何将大型机与运行Windows的PC连接?
- windows – 无法从网络“0xc0000006”启动应用程序
- 为Windows 2012 CA生成新的自签名CA.
- windows-server-2008 – 如何将使用防火墙连接到MS SQL Ser
- powershell – 如何将Windows GUI添加到Windows 2016 Serve
- Elastic Kibana - Install as windows service
- windows-server-2008 – 如何在Windows Server 2
- DevExpress20:XtraCharts控件实现图表
- 如何在Windows中获取当前数量的交互式用户会话?
- 在Windows上Chrome的非标准字体渲染不佳
- 如何在WDS部署的专门化传递期间运行PowerShell脚
- windows Service 2012 系统时间格式带中文
- Windows – 将批处理文件重定向到文件
- windows-server-2008 – 两个本地网络,但Windows
- windows-phone-7 – WP7 – 根据设置动态更改启动