欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#判断网络连接状态

程序员文章站 2024-02-26 09:27:10
运行效果 复制代码 代码如下:using system;using system.collections.generic;using system.text;using...

运行效果

c#判断网络连接状态

复制代码 代码如下:

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