/// <summary>
/// 获取主机是否有网络
/// </summary>
/// <param name="IsPing">是否需要ping 百度地址</param>
/// <param name="PingTimeOut">等待数据返回时间 ,如果超时未接收到时间 ,代表无法访问外部网络</param>
/// <returns></returns>
public static bool GetHostNetWork(bool IsPing = true ,int PingTimeOut = 5000)
{
string Host = "www.baidu.com";
try
{
int dwFlag = 0;
if (!InternetGetConnectedState(ref dwFlag, 0))
{
return false;
}
IPAddress[] addresslist = Dns.GetHostAddresses(Host);
if (addresslist[0].ToString().Length <= 6)
{
return false;
}
if (IsPing)
{
Ping p = new Ping();
PingReply reply = p.Send(Host, PingTimeOut);
if (reply.Status == IPStatus.Success)
{
return true;
}
else
{
return false;
}
}
}
catch
{
return false;
}
return true;
}
c# 判断PC是否连接外网
于 2020-06-30 09:29:59 首次发布