我们的应用程序使用 RSS 从互联网下载数据,但在连接 3G 的计算机上出现连接问题。我们想要检测 3G、EDGE、GPRS 连接,以便我们可以更改应用程序行为、显示警告或连接状态。
这将如何完成?
The NetworkInterface http://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkinterface.aspx类在System.Net.NetworkInformation
命名空间应该对您有一些用处(更具体地说,GetAllNetworkInterfaces http://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkinterface.getallnetworkinterfaces.aspx方法。链接的 MSDN 页面上显示了一个示例,演示了如何获取有关每个网络接口的类型、地址、操作状态和其他信息。
MSDN 示例的简化版本:
public static void ShowNetworkInterfaces()
{
IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
Console.WriteLine("Interface information for {0}.{1} ",
computerProperties.HostName, computerProperties.DomainName);
if (nics == null || nics.Length < 1)
{
Console.WriteLine(" No network interfaces found.");
return;
}
Console.WriteLine(" Number of interfaces .................... : {0}", nics.Length);
foreach (NetworkInterface adapter in nics)
{
IPInterfaceProperties properties = adapter.GetIPProperties();
Console.WriteLine();
Console.WriteLine(adapter.Description);
Console.WriteLine(String.Empty.PadLeft(adapter.Description.Length,'='));
Console.WriteLine(" Interface type .......................... : {0}", adapter.NetworkInterfaceType);
Console.WriteLine(" Physical Address ........................ : {0}",
adapter.GetPhysicalAddress().ToString());
Console.WriteLine(" Operational status ...................... : {0}",
adapter.OperationalStatus);
Console.WriteLine();
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)