优雅/简单的方法来检查网络是否可用与否 [英] An elegant / simple way to check whether internet is available or not

查看:184
本文介绍了优雅/简单的方法来检查网络是否可用与否的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了如何检查网络是否可用不是一个快速搜索。他们中的大多数谈到做互操作调用的wininet.dll。

答案之一指向了System.Net.NetworkInformation命名空间。探索我发现了一类平它可以用来侦测从code我们的服务器的命名空间,并检查服务器是否可用。

我想问的是该解决方案如何比较其他的解决方案?

 平soPing =新平();
变种soPingReply = soPing.Send(www.stackoverflow.com);
如果(soPingReply.Status!= IPStatus.Success)
{
    // SO不可用
}
 

解决方案

试试这个:

  [的DllImport(的wininet.dll)
私有外部静态布尔的InternetGetConnectedState(出int描述,诠释ReservedValue);

//创建一个使用API​​函数的函数...
公共静态布尔IsConnectedToInternet()
{
    诠释说明;
    返回的InternetGetConnectedState(出说明,0);
}
 

I did a quick search on how to check whether Internet is available or not. Most of them talked about making InterOp calls to wininet.dll.

One of the answers pointed towards System.Net.NetworkInformation namespace. Exploring the namespace I found a class Ping which could be used to pinging to our servers from code, and checking whether server is available or not.

What I want to ask is how this solution compares to other solutions?

Ping soPing = new Ping();
var soPingReply = soPing.Send("www.stackoverflow.com");
if (soPingReply.Status != IPStatus.Success)
{
    // SO not available
}

解决方案

Try this:

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

这篇关于优雅/简单的方法来检查网络是否可用与否的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆