Friday, June 11, 2010

Detect Changes in Network Connectivity

Some times you will need a mechanism to check whether changes to network occurring during running your application.
So as a solution for this you can add handlers to the static NetworkAddressChanged and NetworkAvailabilityChanged events implemented by the System.Net.NetworkInformation.NetworkChange class.

Souce Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.NetworkInformation;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Add the handlers to the NetworkChange events.
            NetworkChange.NetworkAvailabilityChanged +=
            NetworkAvailabilityChanged;
            NetworkChange.NetworkAddressChanged +=
            NetworkAddressChanged;

            Console.ReadLine();
        }

        // Declare a method to handle NetworkAvailabilityChanged events.
        private static void NetworkAvailabilityChanged(
        object sender, NetworkAvailabilityEventArgs e)
        {
            // Report whether the network is now available or unavailable.
            if (e.IsAvailable)
            {
                Console.WriteLine("Network Available");
            }
            else
            {
                Console.WriteLine("Network Unavailable");
            }
        }
        // Declare a method to handle NetworkAdressChanged events.
        private static void NetworkAddressChanged(object sender, EventArgs e)
        {
            Console.WriteLine("Current IP Addresses:");
            // Iterate through the interfaces and display information.
            foreach (NetworkInterface ni in
            NetworkInterface.GetAllNetworkInterfaces())
            {
                foreach (UnicastIPAddressInformation addr
                in ni.GetIPProperties().UnicastAddresses)
                {
                    Console.WriteLine(" - {0} (lease expires {1})",
                    addr.Address, DateTime.Now +
                    new TimeSpan(0, 0, (int)addr.DhcpLeaseLifetime));
                }
            }
        }
    }
}

Download Source Code
http://www.4shared.com/file/gyfzpCB_/ConsoleApplication1.html

Monday, June 7, 2010

Obtain Information About the Local Network Interface

Here you will learn ,How to obtain information about the network adapters and network configuration of the local machine.

Code section for application

/****************************************
 * Coded By : D. E Sandaruwan           *
 * Email : erandika1986@gmail.com       *
 * Skype : erandika.sandaruwan          *
 * **************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.NetworkInformation;

namespace GetNetWorkAdaInfor
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnGetInfo_Click(object sender, EventArgs e)
        {
            //Only proceed if there is a network available
            if (NetworkInterface.GetIsNetworkAvailable())
            {
                //Get the set off all NetworkInterface objects for the local machine
                NetworkInterface[] inteface = NetworkInterface.GetAllNetworkInterfaces();

                rtxtInfo.Text = "======================================================================" + "\n";

                //Iterate through the interface and display information
                foreach (NetworkInterface netInter in inteface)
                {
                    //Print basic inteface info
                    rtxtInfo.Text = rtxtInfo.Text + "Interface Name : " + netInter.Name + "\n";
                    rtxtInfo.Text = rtxtInfo.Text + "Description    : " + netInter.Description + "\n";
                    rtxtInfo.Text = rtxtInfo.Text + "ID             : " + netInter.Id + "\n";
                    rtxtInfo.Text = rtxtInfo.Text + "Type           : " + netInter.NetworkInterfaceType + "\n";
                    rtxtInfo.Text = rtxtInfo.Text + "Speed          : " + netInter.Speed + "\n";
                    rtxtInfo.Text = rtxtInfo.Text + "Status         : " + netInter.OperationalStatus + "\n";

                    //report physical address
                    rtxtInfo.Text = rtxtInfo.Text + "Physical address : " + netInter.GetPhysicalAddress().ToString() + "\n";

                    //report network statistics for the interface.
                    rtxtInfo.Text = rtxtInfo.Text + "Byte Sent      : " + netInter.GetIPv4Statistics().BytesSent + "\n";

                    rtxtInfo.Text = rtxtInfo.Text + "Byte Received  : " + netInter.GetIPv4Statistics().BytesReceived + "\n" + "\n" + "\n";

                    //Report IP configuration
                    rtxtInfo.Text = rtxtInfo.Text + "IP Address     : " + "\n";

                    foreach (UnicastIPAddressInformation addr in netInter.GetIPProperties().UnicastAddresses)
                    {
                        rtxtInfo.Text = rtxtInfo.Text + addr.Address + " , Lease expires : " + DateTime.Now + new TimeSpan(0, 0, (int)addr.DhcpLeaseLifetime) + "\n";
                    }

                    rtxtInfo.Text = rtxtInfo.Text + "======================================================================" + "\n";
                }
            }
            else
            {
                rtxtInfo.Text = "No network available in this machine.";
            }
        }
    }
}



Download Source Code here