zoukankan      html  css  js  c++  java
  • C#判断当前的网络是否可用

    判断当前的网络是否可用

    网络连接不禁用时 修改ip网段 这时网络状态显示可通 网页无法打开时

        System.Net.NetworkInformation.Ping ping;
            System.Net.NetworkInformation.PingReply res;

      ping = new System.Net.NetworkInformation.Ping();
                try
                {
                    res = ping.Send("www.baidu.com");  //
                    if (res.Status != System.Net.NetworkInformation.IPStatus.Success)
                    {
                        //没有联上Internet哦,做下自己想做个事情咯
                        MessageBox.Show("0");
                    }
                    else
                    {
                        MessageBox.Show("1");
                    }
                }
                catch(Exception er)
                {
                    //里你,仲系当你断网咯,做下自己想做个事情咯
                    MessageBox.Show("0");
                }
    ————————————————
    版权声明:本文为CSDN博主「费劲_奋进」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/jixiaomeng821/article/details/41896481

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Net.NetworkInformation;
     
    namespace ConsoleApplication1
    {
        class Program
        {
            public static void Main(string[] args)
            {
                string url = "www.baidu.com;www.sina.com;www.cnblogs.com;www.google.com;www.163.com;www.csdn.com";
                string[] urls = url.Split(new char[] { ';' });
                CheckServeStatus(urls);
     
                Console.ReadKey();
            }
            
            /// <summary>
            /// 检测网络连接状态
            /// </summary>
            /// <param name="urls"></param>
            public static void CheckServeStatus(string[] urls)
            {
                int errCount = 0;//ping时连接失败个数
     
                if (!LocalConnectionStatus())
                {
                    Console.WriteLine("网络异常~无连接");
                }
                else if (!MyPing(urls, out errCount))
                {
                    if ((double)errCount / urls.Length >= 0.3)
                    {
                        Console.WriteLine("网络异常~连接多次无响应");
                    }
                    else
                    {
                        Console.WriteLine("网络不稳定");
                    }
                }
                else
                {
                    Console.WriteLine("网络正常");
                }
            }
     
            #region 网络检测
     
             private const int INTERNET_CONNECTION_MODEM = 1;
            private const int INTERNET_CONNECTION_LAN = 2;
     
            [System.Runtime.InteropServices.DllImport("winInet.dll")]
            private static extern bool InternetGetConnectedState(ref int dwFlag, int dwReserved);
     
            /// <summary>
            /// 判断本地的连接状态
             /// </summary>
            /// <returns></returns>
            private static bool LocalConnectionStatus()
            {
                System.Int32 dwFlag = new Int32();
                if (!InternetGetConnectedState(ref dwFlag, 0))
                {
                    Console.WriteLine("LocalConnectionStatus--未连网!");
                    return false;
                }
                else
                {
                    if ((dwFlag & INTERNET_CONNECTION_MODEM) != 0)
                    {
                        Console.WriteLine("LocalConnectionStatus--采用调制解调器上网。");
                        return true;
                    }
                    else if ((dwFlag & INTERNET_CONNECTION_LAN) != 0)
                    {
                        Console.WriteLine("LocalConnectionStatus--采用网卡上网。");
                        return true;
                    }
                }
                return false;
            }
     
            /// <summary>
            /// Ping命令检测网络是否畅通
            /// </summary>
            /// <param name="urls">URL数据</param>
            /// <param name="errorCount">ping时连接失败个数</param>
            /// <returns></returns>
            public static bool MyPing(string[] urls, out int errorCount)
            {
                bool isconn = true;
                Ping ping = new Ping();
                errorCount = 0;
                try
                {
                    PingReply pr;
                    for (int i = 0; i < urls.Length; i++)
                    {
                        pr = ping.Send(urls[i]);
                        if (pr.Status != IPStatus.Success)
                        {
                            isconn = false;
                            errorCount++;
                        }
                        Console.WriteLine("Ping " + urls[i] + "    " + pr.Status.ToString());
                    }
                }
                catch
                {
                    isconn = false;
                    errorCount = urls.Length;
                }
                //if (errorCount > 0 && errorCount < 3)
                //  isconn = true;
                return isconn;
            }
     
            #endregion
        }
    }

    本文来自博客园,作者:云辰,转载请注明原文链接:https://www.cnblogs.com/yunchen/p/12038698.html

  • 相关阅读:
    ES6 语法详解(Promise对象(重点))
    ES6 语法详解(形参默认值)
    ES6 语法详解(三点运算符)
    ES6 语法详解(箭头函数(重点))
    ES6 语法详解(简化的对象写法)
    ES6 语法详解(模板字符串)
    ES6 语法详解(变量的解构赋值)
    [刷题] 走迷宫
    [bug] A reference cannot be reassigned, so it must be initialized at the member-initialization-list.
    [Qt] LibreCAD 源码分析
  • 原文地址:https://www.cnblogs.com/yunchen/p/12038698.html
Copyright © 2011-2022 走看看