了解机器何时连接到Internet(事件)

时间:2013-05-23 20:56:18

标签: c# .net winforms forms sockets

我正在创建一个Win Forms应用程序(C#)。我需要让它保持全天候运行并在连接到Internet时提交一些XML数据。

我知道有几种方法可以ping一个可靠的域来检查系统是否连接到Internet。

但我正在寻找一些更好或更优雅的方法来做同样的事情。我有一些提示,我可以捕获一些套接字事件。请帮我一些想法。

1 个答案:

答案 0 :(得分:1)

请参阅:How to test if Webbrowser gets a connection error when navigating to a new URL?

[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 ) ;
}
}

您可以获得有关函数here的更多信息。