Querying Network Status in .NET programming

TOC

Work with `My` namespace

We can use My.Computer.Network.IsAvailable in Visual Basic.NET to determine whether the network is connected. The definition of this property is:

        '''**********************************************************************
        ''';IsAvailable
        ''' <summary>
        '''  Indicates whether or not the local machine is connected to an IP network.
        ''' </summary>
        ''' <value>True if connected, otherwise False</value>
        ''' <remarks></remarks>
        Public ReadOnly Property IsAvailable() As Boolean
            Get

                Return NetInfoAlias.NetworkInterface.GetIsNetworkAvailable()

            End Get
        End Property

It leads to the approach in C#.

Using `NetworkInterface` class

We can also use `System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable` static function to achieve the same goal.

The function is described in MSDN as follows:

A network connection is considered to be available if any network interface is marked “up” and is not a loopback or tunnel interface.

Using `InternetGetConnectedState` API

A function exposed from Wininet.dll can be used to query network connectivity info. Detialed information is here: InternetGetConnectedState function。The definition of this function is

BOOL InternetGetConnectedState(
  _Out_  LPDWORD lpdwFlags,
  _In_   DWORD dwReserved
);

It returns `TRUE` if there exist available internet connections. Thus we can compose the following code in C# to check it out:

using System;
using System.Runtime.InteropServices;

namespace ConsoleApplication2
{
    class Program
    {
        [Flags]
        public enum INTERNET_CONNECTION : int
        {
            MODEM = 0x01,
            LAN = 0x02,
            PROXY = 0x04,
            MODEM_BUSY = 0x08,
            RAS_INSTALLED = 0x10,
            OFFLINE = 0x20,
            CONFIGURED = 0x40,
        }

        [DllImport("winInet")]
        private static extern bool InternetGetConnectedState(out INTERNET_CONNECTION dwFlag, int dwReserved);

        static void Main(string[] args)
        {
            INTERNET_CONNECTION flag;
            if (InternetGetConnectedState(out flag, 0))
                Console.WriteLine("Network connected: {0}。", flag);
            else
                Console.WriteLine("Network disconnected.");
        }
    }
}

Using Ping

We can also determine whether the machine is online by executing ping test to specific sites. Its advantage lies in that if the result is yes, then we can be 100% sure the network is available.

This method can be implemented by using `System.Net.NetworkInformation.Ping`. You can consult MSDN to learn how to realize it.

Leave a Reply

Your email address will not be published. Required fields are marked *

ERROR: si-captcha.php plugin: GD image support not detected in PHP!

Contact your web host and ask them to enable GD image support for PHP.

ERROR: si-captcha.php plugin: imagepng function not detected in PHP!

Contact your web host and ask them to enable imagepng for PHP.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Content is available under CC BY-SA 3.0 unless otherwise noted.