代码之家  ›  专栏  ›  技术社区  ›  Matthew

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

  •  2
  • Matthew  · 技术社区  · 16 年前

    我们的应用程序使用RSS从Internet下载数据,但在连接3G的机器上存在连接问题。我们希望检测3G、EDGE、GPRS连接,以便更改应用程序行为、显示警告或连接状态。

    怎么做?

    1 回复  |  直到 16 年前
        1
  •  2
  •   Noldorin    16 年前

    这个 NetworkInterface 类中 System.Net.NetworkInformation 名称空间应该对您有所帮助(更具体地说,是 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();
        }
    }