-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnectCheck.cs
100 lines (94 loc) · 3.01 KB
/
ConnectCheck.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Net.NetworkInformation;
namespace AutoDialUp
{
public class GetInternetConStatus
{
private const int INTERNET_CONNECTION_MODEM = 1;
private const int INTERNET_CONNECTION_LAN = 2;
[DllImport("winInet.dll")]
private static extern bool InternetGetConnectedState(
ref int dwFlag,
int dwReserved
);
/// <summary>
/// 判断网络的连接状态
/// </summary>
/// <returns>
/// 网络状态(1-->未联网;2-->采用调制解调器上网;3-->采用网卡上网;4-->采用调制解调器上网但ping不通指定网站;5-->采用网卡上网但ping不通指定网站)
///</returns>
public static int GetNetConStatus(string strNetAddress)
{
int iNetStatus = 0;
System.Int32 dwFlag = new int();
if (!InternetGetConnectedState(ref dwFlag, 0))
{
//没有能连上互联网
iNetStatus = 1;
}
else if ((dwFlag & INTERNET_CONNECTION_MODEM) != 0)
{
//采用调制解调器上网,需要进一步判断能否登录具体网站
if (PingNetAddress(strNetAddress))
{
//可以ping通给定的网址,网络OK
iNetStatus = 2;
}
else
{
//不可以ping通给定的网址,网络不OK
iNetStatus = 4;
}
}
else if ((dwFlag & INTERNET_CONNECTION_LAN) != 0)
{
//采用网卡上网,需要进一步判断能否登录具体网站
if (PingNetAddress(strNetAddress))
{
//可以ping通给定的网址,网络OK
iNetStatus = 3;
}
else
{
//不可以ping通给定的网址,网络不OK
iNetStatus = 5;
}
}
return iNetStatus;
}
/// <summary>
/// ping 具体的网址看能否ping通
/// </summary>
/// <param name="strNetAdd"></param>
/// <returns></returns>
private static bool PingNetAddress(string strNetAdd)
{
bool Flage = false;
Ping ping = new Ping();
try
{
PingReply pr = ping.Send(strNetAdd, 1500);
if (pr.Status == IPStatus.TimedOut)
{
Flage = false;
}
if (pr.Status == IPStatus.Success)
{
Flage = true;
}
else
{
Flage = false;
}
}
catch
{
Flage = false;
}
return Flage;
}
}
}