函数原型:函数InternetGetConnectedState返回本地系统的网络连接状态。

语法:

BOOL InternetGetConnectedState(
__out LPDWORD lpdwFlags,
__in DWORD dwReserved
);
参数:
lpdwFlags[out]
指向一个变量,该变量接收连接描述内容。该参数在函数返回FLASE时仍可以返回一个有效的标记。该参数可以为下列值的一个或多个
含义
INTERNET_CONNECTION_CONFIGURED  
0x40(64)
Local system has a valid connection to the Internet, but it might or might not be currently connected.
INTERNET_CONNECTION_LAN                
  0x02(2)
Local system uses a local area network to connect to the Internet.
INTERNET_CONNECTION_MODEM0          
  x01(1)
Local system uses a modem to connect to the Internet.
INTERNET_CONNECTION_MODEM_BUSY0  
x08(8)
No longer used.
INTERNET_CONNECTION_OFFLINE          
  0x20(16)
Local system is in offline mode.
INTERNET_CONNECTION_PROXY            
   0x04(4)
Local system uses a proxy server to connect to the Internet.
INTERNET_RAS_INSTALLED                    
   0x10(8)
Local system has RAS installed.
dwReserved[in]
保留值。必须为0。
当存在一个modem或一个LAN连接时,返回TRUE,当不存在internet连接或所有的连接当前未被激活时,返回false。
当该函数返回false时,程序可以调用GetLastError来接收错误代码。

运行效果:

代码:

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.NetworkInformation; namespace ConsoleApplication1
{
class Program
{
public static void Main(string[] args)
{
string url = "www.baidu.com;www.sina.com;www.cnblogs.com;www.google.com;www.163.com;www.csdn.com";
string[] urls = url.Split(new char[] { ';' });
CheckServeStatus(urls); Console.ReadKey();
} /// <summary>
/// 检测网络连接状态
/// </summary>
/// <param name="urls"></param>
public static void CheckServeStatus(string[] urls)
{
int errCount = 0;//ping时连接失败个数 if (!LocalConnectionStatus())
{
Console.WriteLine("网络异常~无连接");
}
else if (!MyPing(urls, out errCount))
{
if ((double)errCount / urls.Length >= 0.3)
{
Console.WriteLine("网络异常~连接多次无响应");
}
else
{
Console.WriteLine("网络不稳定");
}
}
else
{
Console.WriteLine("网络正常");
}
} #region 网络检测 private const int INTERNET_CONNECTION_MODEM = 1;
private const int INTERNET_CONNECTION_LAN = 2; [System.Runtime.InteropServices.DllImport("winInet.dll")]
private static extern bool InternetGetConnectedState(ref int dwFlag, int dwReserved); /// <summary>
/// 判断本地的连接状态
/// </summary>
/// <returns></returns>
private static bool LocalConnectionStatus()
{
System.Int32 dwFlag = new Int32();
if (!InternetGetConnectedState(ref dwFlag, 0))
{
Console.WriteLine("LocalConnectionStatus--未连网!");
return false;
}
else
{
if ((dwFlag & INTERNET_CONNECTION_MODEM) != 0)
{
Console.WriteLine("LocalConnectionStatus--采用调制解调器上网。");
return true;
}
else if ((dwFlag & INTERNET_CONNECTION_LAN) != 0)
{
Console.WriteLine("LocalConnectionStatus--采用网卡上网。");
return true;
}
}
return false;
} /// <summary>
/// Ping命令检测网络是否畅通
/// </summary>
/// <param name="urls">URL数据</param>
/// <param name="errorCount">ping时连接失败个数</param>
/// <returns></returns>
public static bool MyPing(string[] urls, out int errorCount)
{
bool isconn = true;
Ping ping = new Ping();
errorCount = 0;
try
{
PingReply pr;
for (int i = 0; i < urls.Length; i++)
{
pr = ping.Send(urls[i]);
if (pr.Status != IPStatus.Success)
{
isconn = false;
errorCount++;
}
Console.WriteLine("Ping " + urls[i] + " " + pr.Status.ToString());
}
}
catch
{
isconn = false;
errorCount = urls.Length;
}
//if (errorCount > 0 && errorCount < 3)
// isconn = true;
return isconn;
} #endregion
}
}
 

使用InternetGetConnectedState判断本地网络状态(C#举例)的更多相关文章

  1. 使用JS在客户端判断当前网络状态

    1. navigator.onLine 2. ajax请求 3. 获取网络资源 1. navigator.onLine 通过navigator.onLine判断当前网络状态: 12345 if(nav ...

  2. iOS 判断当前网络状态的三种方法

    http://www.cocoachina.com/ios/20171103/21039.html 在项目中,为了好的用户体验,有些场景必须线判断网络状态,然后才能决定改干嘛.比如视频播放,需要线判断 ...

  3. android判断当前网络状态及跳转到设置界面

    今天,想做这个跳转到网络设置界面, 刚开始用 intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS); 不料老是出现settings.Wirele ...

  4. C#检测本地网络状态

    using System; using System.Runtime.InteropServices; public static class NetTool { [Flags] private en ...

  5. Android之判断当前网络状态

    /** * 检测网络是否可用 * @return */ public boolean isNetworkConnected() { ConnectivityManager cm = (Connecti ...

  6. Android杂谈--网络状态判断

    许多联网应用都在开始运行的时候检查当前网络状态,如果没有开启则去开启它,记录一下以前写程序时的网络检查,发现人的记忆力真是有限,总是隔段时间久忘记,所以记录下来是最好的记忆. 我们可以在一开始启动程序 ...

  7. linux c 检测网络状态

    转自:http://stackoverflow.com/questions/808560/how-to-detect-the-physical-connected-state-of-a-network ...

  8. Android中判断当前网络是否可用

    转载原文地址:http://www.cnblogs.com/renqingping/archive/2012/10/18/Net.html 当前有可用网络,如下图: 当前没有可用网络,如下图: 实现步 ...

  9. 如何消除原生Android网络状态上的惊叹号

    喜欢使用原生Android系统的朋友可能会发现自己的状态栏信号图标上经常有一个惊叹号标志. 这是怎么回事呢?原因是Android为了对网络状态进行检测,采用了一种叫做captive detection ...

随机推荐

  1. sublime 之 vitage/emmet

    ctrl+shift+p 打开Package Controlctrl+shift+t 恢复已关闭标签ctrl+/ 代码注释/取消注释 --------------------------------- ...

  2. 如何将baidu地图中的baidu logo 去掉

    今天我的老大问我是不是可以将baidumap 的js版中baidu logo 去掉.我上网查询一下,有各种方法,比如将对应的logo div remove hide 等等,这些都是需要JS 函数触发执 ...

  3. [zookeeper] Zookeeper伪分布式集群配置

    参考配置:http://blog.csdn.net/clementad/article/details/48057059 注意要点: 启动时注意分别启动 bin/zkServer.sh start c ...

  4. java 正则表达式 验证字符串 只包含汉字英文数字

    String content = “testContent”; String regex="^[a-zA-Z0-9\u4E00-\u9FA5]+$"; Pattern patter ...

  5. 【转】【WPF】WPF中MeasureOverride ArrangeOverride 的理解

    1. Measure Arrange这两个方法是UIElement的方法 MeasureOverride ArrangeOverride这两个方法是FrameworkElement的方法,Framew ...

  6. 【转载】Exchange 2010配置与安装实用手册

    Exchange 2010配置与安装实用手册 在Exchange 2010配置的时候主要分三大部分,这分别是网络配置.准备存储以及相关的安装策略和过程.同时还需要注意和其他的Windows软件相协调. ...

  7. android 8 wifi 信号等级

    wifi信号等级 /frameworks/base/wifi/java/android/net/wifi/wifimanager.java calculateSignalLevel为计算信号等级函数, ...

  8. Redis工业生产应用场景

    Redis应用场景 国内外三个不同领域巨头分享的Redis实战经验及使用场景 Redis的5个常见使用场景 Redis应用场景 Redis应用场景<张善友>

  9. Winform控件学习笔记【第三天】——ListBox

    1. 属性事件列表: SelectionMode    组件中条目的选择类型,即多选(Multiple).单选(Single) Rows             列表框中显示总共多少行 Selecte ...

  10. C# wkhtmltopdf 将html转pdf

    一.转换程序代码如下: public string HtmlToPdf(string url) { bool success = true; // string dwbh = url.Split('? ...