News
  • Favorite
  • Facebook
  • Twitter
  • LinkedIn

Microsoft Press Pearson Education (InformIT)
Skip Navigation Links
Pearson Education (InformIT)
How to: Detect Network Connectivity Availability Change Status using C#

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:

1.       Create a new windows application using Visual Studio 2005/2008/2010.

2.       Rename Form1 to NetworkForm.

3.       Add a ToolStrip control to the NetworkForm and rename it to NetworkToolStrip.

4.       Add a ToolStripLabel to the NetworkToolStrip and rename it to NetworkAvailabilityToolStripLabel and set its Text property to Network Availability.

5.       Right click the NetworkAvailabilityToolStripLabel and select Alignment and click Right. This will align the NetworkAvailabilityToolStripLabel to the Right side.

6.       Right click the NetworkAvailabilityToolStripLabel and select DisplayStyle and select ImageAndText. This will display the image beside the text.

7.       Right click the NetworkAvailabilityToolStripLabel and select Set Image. This will Display the Select Resource Dialog Box.

NetworkAvailability_02

8.       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.

9.       After adding the two images select one of them as the default image that will be displayed.

10.   Import the System.Net and System.Net.NetworkInformation namespaces using the following statement:

using System.Net;

using System.Net.NetworkInformation;

 

11.   The System.Net namespace provides a simple programming interface for many of the protocols used on networks today.

12.   The System.Net.NetworkInformation namespace provides access to network traffic data, network address information, and notification of address changes for the local computer.

13.   Add the following code to the constructor of the NetworkForm class.

public NetworkForm()

{

    NetworkChange.NetworkAvailabilityChanged +=

        new NetworkAvailabilityChangedEventHandler(NetworkChange_NetworkAvailabilityChanged);

}

 

a.       This code snippet simply registers an event handler for the NetworkAvailabilityChanged event in the NetworkChange class.

b.      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.

c.       NetworkAvailabilityChanged event occurs when the availability of the network changes.

14.   Add the following code to the NetworkChange_NetworkAvailabilityChanged event handler created in the previous step:

private void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)

{

    try

    {

        if (e.IsAvailable)

        {

            NetworkAvailabilityToolStripLabel.Image = Properties.Resources._023;

            NetworkAvailabilityToolStripLabel.ToolTipText = "Network is Available";

        }

        else

        {

            NetworkAvailabilityToolStripLabel.Image = Properties.Resources._013;

            NetworkAvailabilityToolStripLabel.ToolTipText = "Network is not Available";

        }

    }

    catch (NetworkInformationException ex)

    {

        MessageBox.Show(ex.Message, ex.GetType().ToString(),

            MessageBoxButtons.OK, MessageBoxIcon.Error);

    }

    catch (Exception ex)

    {

        MessageBox.Show(ex.Message, ex.GetType().ToString(),

            MessageBoxButtons.OK, MessageBoxIcon.Error);

    }

    finally

    {

    }

}

 

a.       The NetworkAvailabilityEventArgs Class contains one property IsAvailable that we will use to check if the network is available or not.

b.      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 following text "Network is Available".

c.       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".

d.      We then catch the NetworkInformationException and display error message in a MessageBox. This exception is thrown when an error occurs while retrieving network information.

15.   Build and run your application.

16.   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

NetworkAvailability_02

17.   Try to disconnect your machine from the network the NetworkAvailabilityToolStripLabel image and tooltip will be changed as shown in the following image.

 

NetworkAvailability_02

Now you have an application that detects network availability change.

(129) Comments

Blog Message https://disqus.com/by/vancouver_seo/

Wow, great blog.Really thank you! Will read on


Blog Message https://foursquare.com/user/556078354/list/choosing-the-best-bathroom-sink

Very good article post.Thanks Again. Cool.


Blog Message https://seovancouver.net/

Very interesting topic , thanks for putting up.


Blog Message https://zenwriting.net/springgrey7/adobe-flash-web-design-marvel

This website was how do you say it? Relevant!! Finally I ave found something that helped me. Appreciate it!


Blog Message https://toledobendclassifieds.com/blog/author/gymslice23/

There exists noticeably a bundle to comprehend this. I suppose you might have made distinct good points in features also.


Blog Message https://nairaoutlet.com/

It is in reality a nice and helpful piece of information. I am satisfied that you shared this helpful info with us. Please stay us up to date like this. Thanks for sharing.


Blog Message https://www.bookmaker-toto.com

Say, you got a nice post.Really looking forward to read more. Much obliged.


Blog Message http://medical-beauty.org.ua/��-����������-�����

Keep up the superb piece of work, I read few posts on this website and I believe that your web site is really interesting and has got circles of superb information.


Blog Message https://www.kouponkabla.com/autobell-coupon-2019-now-get-latest

It is a beautiful shot with very good light


Blog Message https://www.kouponkabla.com/stockx-discount-code-seller-2019-for-all

Maybe You Also Make All of these Mistakes With bag ?


Blog Message https://www.nosh121.com/32-off-freetaxusa-com-new-coupon-codes-discounts

This blog is really awesome as well as diverting. I have chosen many useful things out of this amazing blog. I ad love to visit it every once in a while. Thanks a lot!


Blog Message https://seovancouver.net/2019/07/24/seo-vancouver/

please go to the sites we follow, such as this a single, as it represents our picks from the web


Blog Message https://www.youtube.com/watch?v=B02LSnQd13c

That is a very good tip especially to those new to the blogosphere. Short but very accurate info Appreciate your sharing this one. A must read article!


Blog Message https://www.kouponkabla.com/marco-coupon-2019-get-newest-here

Your explanation is the best I have seen so far


Blog Message https://seovancouver.net/

Looking around While I was surfing yesterday I saw a great post about

First Previous 1 2 3 4 5 6 7 8 9 Next Last 
Rating
Name
Email
Comment