标签:
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 }
标签:
原文地址:http://www.cnblogs.com/aiqingqing/p/4553992.html