zoukankan      html  css  js  c++  java
  • C#中检查网络是否连通的二种方法

    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 } 

  • 相关阅读:
    Linux下Samba的配置
    NYoj-街区最短路径问题
    Merge into的使用具体解释-你Merge了没有
    c++11 stl atomic_flag 样例
    7个最好的免费杀毒软件下载
    关于PCA算法的一点学习总结
    linux查看硬件信息
    js php xmlrequest 上传图片
    BZOJ1827 [Usaco2010 Mar]gather 奶牛大集会
    只有小于65535端口编程可以用,查看哪些端口被打开netstat -anp,nc命令,nmap命令
  • 原文地址:https://www.cnblogs.com/aiqingqing/p/4553992.html
Copyright © 2011-2022 走看看