luni, 25 august 2014

Handle NetworkAvailabilityChanged event in C# Windows Forms

Occurs when the availability of the network changes

Example

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());
        }
    }
}

Niciun comentariu:

Trimiteți un comentariu