2010-10-13 107 views
4

我需要一种以编程方式确定Internet可用性的方法。 现在我用Ping来不断ping一些网站。如何确定网络接口是否连接到Internet(即,计算机处于联机状态)

但似乎Windows 7以其他方式决定了网络可用性。 如果电脑在线,则系统托盘中的网络接口图标上有地球图标。

问题是:是否有任何标准的Win32方式来检查在线状态,赢得事件或什么的,如果是的话,如何从C#中使用它?

+0

你打算怎么处理这些信息?由于互联网连接可能在任何时候都会停止,因此无论如何您必须防守地进行编程。 – 2010-10-13 09:54:16

+0

Whell,我有一个代码,重新启用LAN接口。 – skaeff 2010-10-13 10:18:05

+0

它通过WMI禁用LAN接口,然后再次启用 - 当提供者端出现问题时,它会有所帮助。我也打算设置一个WiMax或GSM调制解调器,因为它是非常关键的我的应用程序连接:) – skaeff 2010-10-13 10:18:37

回答

4

我相信尽管你的问题似乎是一个重复像这样的工作:

using System; 
using System.Runtime; 
using System.Runtime.InteropServices; 

public class InternetCS 
{ 
    //Creating the extern function... 
    [DllImport("wininet.dll")] 
    private extern static bool InternetGetConnectedState(out int Description, int ReservedValue); 

    //Creating a function that uses the API function... 
    public static bool IsConnectedToInternet() 
    { 
     int Desc ; 
     return InternetGetConnectedState(out Desc, 0) ; 
    } 
} 

forund这里:

check whether Internet connection is available with C#

2

'连接到Internet' 不具有任何除调制解调器外的实际含义。测试是否有任何资源可用的节拍方式是使用它。无论如何,你必须应对失败,无需对所有代码进行两次编码。

+0

那么,它已经这样做了。每3秒我发一个ping给一个公共的whell-known主机,如果我至少失败10个周期 - 我重新启动网络接口。 – skaeff 2010-10-13 12:10:07

相关问题