Sometimes your application
connects to a web service or web sites or querying databases on other machines
on the network. And sometimes your application does not get results of
operations you performed as your machine disconnects and connects again to the
network. You need your application to detect network availability change.
Fortunately the System.Net.NetworkInformation contains the NetworkChange that
will allow us to detect network availability change.
Using the Code:
To create an application that
detects network availability change follow the following steps:
Create a new windows application using Visual
Rename Form1 to NetworkForm.
Add a ToolStrip control to the
NetworkForm and rename it to NetworkToolStrip.
Add a ToolStripLabel to the
NetworkToolStrip and rename it to
NetworkAvailabilityToolStripLabel and set its Text property to
Right click the
NetworkAvailabilityToolStripLabel and select Alignment and click
Right. This will align the NetworkAvailabilityToolStripLabel to the
Right click the
NetworkAvailabilityToolStripLabel and select DisplayStyle and select
ImageAndText. This will display the image beside the text.
Right click the
NetworkAvailabilityToolStripLabel and select Set Image. This will Display
the Select Resource Dialog Box.
Click on Project resource file Radio
Button and Click on Import button and navigate to two Images that will be
used to indicate network availability.
After adding the two images select one of
them as the default image that will be displayed.
System.Net.NetworkInformation namespaces using the following statement:
The System.Net namespace provides a
simple programming interface for many of the protocols used on networks today.
namespace provides access to network traffic data, network address information,
and notification of address changes for the local computer.
Add the following code to the constructor
This code snippet simply registers an event
handler for the NetworkAvailabilityChanged event in the NetworkChange
NetworkChange Class allows applications to receive notification when
the Internet Protocol (IP) addresses of a network interface, also called a
network card or adapter, changes.
NetworkAvailabilityChanged event occurs when the availability of the network
Add the following code to the
NetworkChange_NetworkAvailabilityChanged event handler created in the
NetworkAvailabilityToolStripLabel.Image = Properties.Resources._023;
"Network is Available";
NetworkAvailabilityToolStripLabel.Image = Properties.Resources._013;
"Network is not Available";
catch (NetworkInformationException ex)
catch (Exception ex)
Class contains one property IsAvailable that we will use to check if the
network is available or not.
We check the IsAvailable property
and if true we set the image of the NetworkAvailabilityToolStripLabel to
one of the two images we added earlier and set the tool tip for the label to the
"Network is Available".
If the IsAvailable property is
false, we set the image of the NetworkAvailabilityToolStripLabel to the
other image and set the tool tip for the label to the following text
"Network is not Available".
We then catch the
NetworkInformationException and display error message in a MessageBox. This
exception is thrown when an error occurs while retrieving network information.
Build and run your application.
If your machine is connected to a network
you will see the NetworkAvailabilityToolStripLabel image and tooltip
indicates you are connected as shown in the following image
Try to disconnect your machine from the
network the NetworkAvailabilityToolStripLabel image and tooltip will be
changed as shown in the following image.
Now you have an application that
detects network availability change.