代码改变世界

判断计算机是否联网

2013-12-10 16:40  yuxianghappy  阅读(195)  评论(0编辑  收藏  举报

/// <summary>
/// 判断是否连接网络
/// </summary>
/// <returns></returns>
public static bool IsInternet()
{
var status = false;
//判断是否联网
try
{
System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
System.Net.NetworkInformation.PingReply res = ping.Send("www.baidu.com");
if (res.Status == System.Net.NetworkInformation.IPStatus.Success)
{
//连接上Internet
status = true;
}
}
catch
{
status = false;
}
return status;
}