1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 //方法一
5 using System.Runtime;
6 using System.Runtime.InteropServices;
7 //方法二 Net2.0新增类库
8 using System.Net.NetworkInformation;
9
10 namespace InternetCheck
11 {
12 public class Internet
13 {
14 [DllImport("wininet.dll")]
15 private extern static bool InternetGetConnectedState(int Description, int ReservedValue);
16
17 #region 方法一
18 /// <summary>
19 /// 用于检查网络是否可以连接互联网,true表示连接成功,false表示连接失败
20 /// </summary>
21 /// <returns></returns>
22 public static bool IsConnectInternet()
23 {
24 int Description = 0;
25 return InternetGetConnectedState(Description, 0);
26 }
27 #endregion
28
29 #region 方法二
30 /// <summary>
31 /// 用于检查IP地址或域名是否可以使用TCP/IP协议访问(使用Ping命令),true表示Ping成功,false表示Ping失败
32 /// </summary>
33 /// <param name="strIpOrDName">输入参数,表示IP地址或域名</param>
34 /// <returns></returns>
35 public static bool PingIpOrDomainName(string strIpOrDName)
36 {
37 try
38 {
39 Ping objPingSender = new Ping();
40 PingOptions objPinOptions = new PingOptions();
41 objPinOptions.DontFragment = true;
42 string data = "";
43 byte[] buffer = Encoding.UTF8.GetBytes(data);
44 int intTimeout = 120;
45 PingReply objPinReply = objPingSender.Send(strIpOrDName, intTimeout, buffer, objPinOptions);
46 string strInfo = objPinReply.Status.ToString();
47 if (strInfo == "Success")
48 {
49 return true;
50 }
51 else
52 {
53 return false;
54 }
55 }
56 catch (Exception)
57 {
58 return false;
59 }
60 }
61 #endregion
62 }
63 }
64 using System;
65 using System.Collections.Generic;
66 using System.Text;
67 //方法一
68 using System.Runtime;
69 using System.Runtime.InteropServices;
70 //方法二 Net2.0新增类库
71 using System.Net.NetworkInformation;
72
73 namespace InternetCheck
74 {
75 public class Internet
76 {
77 [DllImport("wininet.dll")]
78 private extern static bool InternetGetConnectedState(int Description, int ReservedValue);
79
80 #region 方法一
81 /// <summary>
82 /// 用于检查网络是否可以连接互联网,true表示连接成功,false表示连接失败
83 /// </summary>
84 /// <returns></returns>
85 public static bool IsConnectInternet()
86 {
87 int Description = 0;
88 return InternetGetConnectedState(Description, 0);
89 }
90 #endregion
91
92 #region 方法二
93 /// <summary>
94 /// 用于检查IP地址或域名是否可以使用TCP/IP协议访问(使用Ping命令),true表示Ping成功,false表示Ping失败
95 /// </summary>
96 /// <param name="strIpOrDName">输入参数,表示IP地址或域名</param>
97 /// <returns></returns>
98 public static bool PingIpOrDomainName(string strIpOrDName)
99 {
100 try
101 {
102 Ping objPingSender = new Ping();
103 PingOptions objPinOptions = new PingOptions();
104 objPinOptions.DontFragment = true;
105 string data = "";
106 byte[] buffer = Encoding.UTF8.GetBytes(data);
107 int intTimeout = 120;
108 PingReply objPinReply = objPingSender.Send(strIpOrDName, intTimeout, buffer, objPinOptions);
109 string strInfo = objPinReply.Status.ToString();
110 if (strInfo == "Success")
111 {
112 return true;
113 }
114 else
115 {
116 return false;
117 }
118 }
119 catch (Exception)
120 {
121 return false;
122 }
123 }
124 #endregion
125 }
126 }
127
128
129 // 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/yhrch/archive/2007/11/05/1867546.aspx
2 using System.Collections.Generic;
3 using System.Text;
4 //方法一
5 using System.Runtime;
6 using System.Runtime.InteropServices;
7 //方法二 Net2.0新增类库
8 using System.Net.NetworkInformation;
9
10 namespace InternetCheck
11 {
12 public class Internet
13 {
14 [DllImport("wininet.dll")]
15 private extern static bool InternetGetConnectedState(int Description, int ReservedValue);
16
17 #region 方法一
18 /// <summary>
19 /// 用于检查网络是否可以连接互联网,true表示连接成功,false表示连接失败
20 /// </summary>
21 /// <returns></returns>
22 public static bool IsConnectInternet()
23 {
24 int Description = 0;
25 return InternetGetConnectedState(Description, 0);
26 }
27 #endregion
28
29 #region 方法二
30 /// <summary>
31 /// 用于检查IP地址或域名是否可以使用TCP/IP协议访问(使用Ping命令),true表示Ping成功,false表示Ping失败
32 /// </summary>
33 /// <param name="strIpOrDName">输入参数,表示IP地址或域名</param>
34 /// <returns></returns>
35 public static bool PingIpOrDomainName(string strIpOrDName)
36 {
37 try
38 {
39 Ping objPingSender = new Ping();
40 PingOptions objPinOptions = new PingOptions();
41 objPinOptions.DontFragment = true;
42 string data = "";
43 byte[] buffer = Encoding.UTF8.GetBytes(data);
44 int intTimeout = 120;
45 PingReply objPinReply = objPingSender.Send(strIpOrDName, intTimeout, buffer, objPinOptions);
46 string strInfo = objPinReply.Status.ToString();
47 if (strInfo == "Success")
48 {
49 return true;
50 }
51 else
52 {
53 return false;
54 }
55 }
56 catch (Exception)
57 {
58 return false;
59 }
60 }
61 #endregion
62 }
63 }
64 using System;
65 using System.Collections.Generic;
66 using System.Text;
67 //方法一
68 using System.Runtime;
69 using System.Runtime.InteropServices;
70 //方法二 Net2.0新增类库
71 using System.Net.NetworkInformation;
72
73 namespace InternetCheck
74 {
75 public class Internet
76 {
77 [DllImport("wininet.dll")]
78 private extern static bool InternetGetConnectedState(int Description, int ReservedValue);
79
80 #region 方法一
81 /// <summary>
82 /// 用于检查网络是否可以连接互联网,true表示连接成功,false表示连接失败
83 /// </summary>
84 /// <returns></returns>
85 public static bool IsConnectInternet()
86 {
87 int Description = 0;
88 return InternetGetConnectedState(Description, 0);
89 }
90 #endregion
91
92 #region 方法二
93 /// <summary>
94 /// 用于检查IP地址或域名是否可以使用TCP/IP协议访问(使用Ping命令),true表示Ping成功,false表示Ping失败
95 /// </summary>
96 /// <param name="strIpOrDName">输入参数,表示IP地址或域名</param>
97 /// <returns></returns>
98 public static bool PingIpOrDomainName(string strIpOrDName)
99 {
100 try
101 {
102 Ping objPingSender = new Ping();
103 PingOptions objPinOptions = new PingOptions();
104 objPinOptions.DontFragment = true;
105 string data = "";
106 byte[] buffer = Encoding.UTF8.GetBytes(data);
107 int intTimeout = 120;
108 PingReply objPinReply = objPingSender.Send(strIpOrDName, intTimeout, buffer, objPinOptions);
109 string strInfo = objPinReply.Status.ToString();
110 if (strInfo == "Success")
111 {
112 return true;
113 }
114 else
115 {
116 return false;
117 }
118 }
119 catch (Exception)
120 {
121 return false;
122 }
123 }
124 #endregion
125 }
126 }
127
128
129 // 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/yhrch/archive/2007/11/05/1867546.aspx