函數原型:函數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
}
}

