Querying Network Status in .NET programming

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:

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

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

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 *

*

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