zoukankan      html  css  js  c++  java
  • 判断是否断网

    这是一个判断是否断网的工具类:

    //方法一对应的命名空间
    using System.Runtime;
    using System.Runtime.InteropServices;
    //方法二对应的命名空间
    using System.Net.NetworkInformation;

    namespace WindowsFormsApplication1
    {
    /// <summary>
    /// 判断是否连接网络
    /// </summary>
    public static class IsOffTheNet
    {
    #region 方法一
    [DllImport("wininet.dll")]
    private extern static bool InternetGetConnectedState(int Description, int ReservedValue);

    /// <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
    }

  • 相关阅读:
    第194场周赛
    刷leetcode的心得
    91. Decode Ways
    23. Merge k Sorted Lists
    19. Remove Nth Node From End of List
    21. Merge Two Sorted Lists
    222. Count Complete Tree Nodes
    958. Check Completeness of a Binary Tree
    课程学习总结报告
    结合中断上下文切换和进程上下文切换分析Linux内核一般执行过程
  • 原文地址:https://www.cnblogs.com/zhudezhiwansui/p/6397941.html
Copyright © 2011-2022 走看看