• 【转】 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
    复制代码
  • 相关阅读:
    为什么重复值高的字段不能建索引(比如性别字段等)
    【转】微服务架构技术栈选型手册
    【转】使用索引为什么能提高查询速度?
    SpringBoot读取配置文件(从classpath/file读取yml/properties文件)
    集合总结--ArrayList、LinkedList、HashMap
    HashMap问答
    【转】http的keep-alive和tcp的keepalive区别
    【转】大数据分析中Redis怎么做到220万ops
    哪个中年IT男不是一边面对危机,一边咬牙硬抗【转】
    SVN clean失败解决方法【转】
  • 原文地址:https://www.cnblogs.com/bruce1992/p/14881713.html
Copyright © 2020-2023  润新知