SoFunction
Updated on 2025-04-04

C# Determine network connection status


using System;
using ;
using ;
using ;

namespace ConsoleApplication1
{
    class Program
    {
        public static void Main(string[] args)
        {
            string url = ";;;;;";
            string[] urls = (new char[] { ';' });
            CheckServeStatus(urls);

            ();
        }

        /// <summary>
/// Detect network connection status
        /// </summary>
        /// <param name="urls"></param>
        public static void CheckServeStatus(string[] urls)
        {
int errCount = 0;//Number of connection failures during ping

            if (!LocalConnectionStatus())
            {
("Network exception~no connection");
            }
            else if (!MyPing(urls, out errCount))
            {
                if ((double)errCount / >= 0.3)
                {
("Network exception~ connections are unresponsive");
                }
                else
                {
("Network instability");
                }
            }
            else
            {
("Network is normal");
            }
        }

#region Network Detection

         private const int INTERNET_CONNECTION_MODEM = 1;
        private const int INTERNET_CONNECTION_LAN = 2;

        [("")]
        private static extern bool InternetGetConnectedState(ref int dwFlag, int dwReserved);

        /// <summary>
/// Determine the local connection status
         /// </summary>
        /// <returns></returns>
        private static bool LocalConnectionStatus()
        {
            System.Int32 dwFlag = new Int32();
            if (!InternetGetConnectedState(ref dwFlag, 0))
            {
("LocalConnectionStatus--Not connected to the Internet!");
                return false;
            }
            else
            {
                if ((dwFlag & INTERNET_CONNECTION_MODEM) != 0)
                {
("LocalConnectionStatus--Use a modem to surf the Internet.");
                    return true;
                }
                else if ((dwFlag & INTERNET_CONNECTION_LAN) != 0)
                {
("LocalConnectionStatus--uses network card to access the Internet.");
                    return true;
                }
            }
            return false;
        }

        /// <summary>
/// Ping command to detect whether the network is smooth
        /// </summary>
/// <param name="urls">URL data</param>
/// <param name="errorCount">Number of connection failures during 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 < ; i++)
                {
                    pr = (urls[i]);
                    if ( != )
                    {
                        isconn = false;
                        errorCount++;
                    }
                    ("Ping " + urls[i] + "    " + ());
                }
            }
            catch
            {
                isconn = false;
                errorCount = ;
            }
            //if (errorCount > 0 && errorCount < 3)
            //  isconn = true;
            return isconn;
        }

        #endregion
    }
}