优雅/简单的方法来检查网络是否可用与否方法来、优雅、简单、网络

2023-09-07 00:15:49 作者:心若向阳,无畏悲伤

我做了如何检查网络是否可用不是一个快速搜索。他们中的大多数谈到做互操作调用的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);
}
 

数安法 今起施行,由API产生的数据泄露问题这样来解决

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