我需要一种编程方式来确定网络的可用性。
目前我使用Ping来不断地ping一些互联网站。
但是在Windows 7中,它似乎以其他方式确定网络的可用性。如果计算机在线,系统托盘中的网络接口图标上会有地球图标。
问题是:是否有任何标准的Win32方法来检查在线状态,包括事件或其他方法,并且如果有,如何从C#中使用它?
我需要一种编程方式来确定网络的可用性。
目前我使用Ping来不断地ping一些互联网站。
但是在Windows 7中,它似乎以其他方式确定网络的可用性。如果计算机在线,系统托盘中的网络接口图标上会有地球图标。
问题是:是否有任何标准的Win32方法来检查在线状态,包括事件或其他方法,并且如果有,如何从C#中使用它?
我相信这样做是可行的,尽管你的问题似乎是一个重复的问题:
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 ) ;
}
}
在这里找到:
'连接到互联网'除了在调制解调器的情况下没有任何实际意义。测试任何资源是否可用的最佳方法是使用它。无论如何,您都必须在那一点上处理故障,没有必要编写所有内容两次。