【转】 C#中检查网络是否连通的二种方法
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
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析