zoukankan      html  css  js  c++  java
  • C#检查网络是否可以连接互联网

    添加引用:

    using System.Runtime.InteropServices;

    using System.Net.NetworkInformation;

            [DllImport("wininet.dll")]
            private extern static bool InternetGetConnectedState(int Description, int ReservedValue);
    
            #region 方法一
    
            /// <summary>
            /// 用于检查网络是否可以连接互联网,true表示连接成功,false表示连接失败
            /// </summary>
            /// <returns></returns>
            public static bool IsConnectInternet()
            {
                int Description = 0;
                return InternetGetConnectedState(Description, 0);
            }
    
            #endregion 方法一
    
            #region 方法二
    
            /// <summary>
            /// 用于检查IP地址或域名是否可以使用TCP/IP协议访问(使用Ping命令),true表示Ping成功,false表示Ping失败
            /// </summary>
            /// <param name="strIpOrDName">输入参数,表示IP地址或域名</param>
            /// <returns></returns>
            public static bool PingIpOrDomainName(string strIpOrDName)
            {
                try
                {
                    Ping objPingSender = new Ping();
                    PingOptions objPinOptions = new PingOptions();
                    objPinOptions.DontFragment = true;
                    string data = "";
                    byte[] buffer = Encoding.UTF8.GetBytes(data);
                    int intTimeout = 120;
                    PingReply objPinReply = objPingSender.Send(strIpOrDName, intTimeout, buffer, objPinOptions);
                    string strInfo = objPinReply.Status.ToString();
                    if (strInfo == "Success")
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch (Exception)
                {
                    return false;
                }
            }
    
            #endregion 方法二

    调用我就不写了,相信大家都会。

  • 相关阅读:
    python 计时累积超过24小时时继续往上累加
    linux 下获取文件最后几行
    unbuntu 安装python包提示E: Unable to locate package python-timeout
    python 计时器
    jquery中html()、text()、val()的区别
    DESC和 ACS
    jQuery自动截取文字长度,超过部分
    Spring MVC 注解
    注解笔记
    Spring Data JPA初使用 *****重要********
  • 原文地址:https://www.cnblogs.com/mq0036/p/6394705.html
Copyright © 2011-2022 走看看