在.NET中检测3G互联网连接

时间:2009-07-04 10:50:20

标签: .net networking

我们的应用程序使用RSS从互联网上下载数据,但在连接3G的机器上遇到连接问题。我们想检测3G,EDGE,GPRS连接,以便我们可以更改应用程序行为,显示警告或连接状态。

如何做到这一点?

1 个答案:

答案 0 :(得分:2)

System.Net.NetworkInformation命名空间中的NetworkInterface类应该对您有用(更具体地说,GetAllNetworkInterfaces方法。链接的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();
    }
}