使用InternetGetConnectedState判断本地网络状态(C#举例)
函数原型:函数InternetGetConnectedState返回本地系统的网络连接状态。
语法:
|
值
|
含义
|
|---|---|
|
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.
|
运行效果:

代码:

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#举例)的更多相关文章
- 使用JS在客户端判断当前网络状态
1. navigator.onLine 2. ajax请求 3. 获取网络资源 1. navigator.onLine 通过navigator.onLine判断当前网络状态: 12345 if(nav ...
- iOS 判断当前网络状态的三种方法
http://www.cocoachina.com/ios/20171103/21039.html 在项目中,为了好的用户体验,有些场景必须线判断网络状态,然后才能决定改干嘛.比如视频播放,需要线判断 ...
- android判断当前网络状态及跳转到设置界面
今天,想做这个跳转到网络设置界面, 刚开始用 intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS); 不料老是出现settings.Wirele ...
- C#检测本地网络状态
using System; using System.Runtime.InteropServices; public static class NetTool { [Flags] private en ...
- Android之判断当前网络状态
/** * 检测网络是否可用 * @return */ public boolean isNetworkConnected() { ConnectivityManager cm = (Connecti ...
- Android杂谈--网络状态判断
许多联网应用都在开始运行的时候检查当前网络状态,如果没有开启则去开启它,记录一下以前写程序时的网络检查,发现人的记忆力真是有限,总是隔段时间久忘记,所以记录下来是最好的记忆. 我们可以在一开始启动程序 ...
- linux c 检测网络状态
转自:http://stackoverflow.com/questions/808560/how-to-detect-the-physical-connected-state-of-a-network ...
- Android中判断当前网络是否可用
转载原文地址:http://www.cnblogs.com/renqingping/archive/2012/10/18/Net.html 当前有可用网络,如下图: 当前没有可用网络,如下图: 实现步 ...
- 如何消除原生Android网络状态上的惊叹号
喜欢使用原生Android系统的朋友可能会发现自己的状态栏信号图标上经常有一个惊叹号标志. 这是怎么回事呢?原因是Android为了对网络状态进行检测,采用了一种叫做captive detection ...
随机推荐
- elasticsearch6.4 memory locking requested for elasticsearch process but memory is not locked
[2018-10-18T05:28:57,713][ERROR][o.e.b.Bootstrap ] [node-45] node validation exception[1] bootstrap ...
- WebUpload formdata 上传参数
https://www.cnblogs.com/wisdo/p/6159761.html webUploader 是款很好用的优秀的开源上传组件,由百度公司开发,详细的介绍可参见webUploader ...
- 攻城狮送女友的CSS3生日蛋糕
在线预览:http://keleyi.com/keleyi/phtml/html5/29.htm 代码如下: <!DOCTYPE html> <html> <head&g ...
- 第三百六十节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)的基本概念
第三百六十节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)的基本概念 elasticsearch的基本概念 1.集群:一个或者多个节点组织在一起 2.节点 ...
- e821. 设置JScrollPane滚动栏
A scroll bar in a scroll pane can be set to appear only as needed, always appear, or never appear. B ...
- Git -- 工作区 和 暂存区
Git和其他版本控制系统如SVN的一个不同之处就是有暂存区的概念. 先来看名词解释. 工作区(Working Directory) 就是你在电脑里能看到的目录,比如我的learngit文件夹就是一个工 ...
- Cisco 3550配置DHCP中继代理
实验环境: 1.配置两个VLAN 10 和 VLAN 20 VLAN 10 IP地址设置:192.168.10.1 255.255.255.0 (192.168.10.1是VLAN 10网关 ...
- systemd的运行级别与服务管理命令简介
从很久很久以前我们就在使用静态运行级别.而systemd提供了更为动态灵活的机制,来管控你的系统. 在开始介绍systemd命令前,让我们先简单的回顾一下历史.在Linux世界里,有一个很奇怪的现象, ...
- mothur reverse.seqs 将序列反向互补
reverse.seqs 命令可以得到输入序列的反向互补序列 用法: mothur "#reverse.seqs(fasta = "input.fasta")" ...
- 第一個shell腳本
#!/bin/bash echo "Hello World !" 運行