Search This Blog

Monday, March 25, 2013

Handle NetworkAvailabilityChanged event in C# Windows Forms

C# > System.Net > NetworkInformation > NetworkChange > NetworkAvailabilityChanged

NetworkAvailabilityChanged occurs when the availability of the network changes.

Example

Check network available.

Edit Program.cs: Add event to check network is available






using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    static class Program
    {
        ///
        /// The main entry point for the application.
        ///

        private static void NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
        {
            if (e.IsAvailable)
            {
                MessageBox.Show ("Network Available");
            }
            else
            {
                MessageBox.Show("Network Unavailable");
            }
        }

        [STAThread]
        static void Main()
        {
            NetworkChange.NetworkAvailabilityChanged += NetworkAvailabilityChanged;

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}