tags:

views:

26

answers:

2

I need to get current connection state.

I found: System.Net.NetworkInformation.NetworkChange.NetworkAvailabilityChanged event but how can I get current network state ?

A: 

You are probably looking for the NetworkInterface class.

leppie
A: 

Take a look at the code on MSDN here, http://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkchange.aspx. It show how to query the network adapters to determine their state.

public class NetworkingExample
{
    public static void Main()
    {
        NetworkChange.NetworkAddressChanged += new 
        NetworkAddressChangedEventHandler(AddressChangedCallback);
        Console.WriteLine("Listening for address changes. Press any key to exit.");
        Console.ReadLine();
    }
    static void AddressChangedCallback(object sender, EventArgs e)
    {

        NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
        foreach(NetworkInterface n in adapters)
        {
            Console.WriteLine("   {0} is {1}", n.Name, n.OperationalStatus);
        }
    }
}
Chuck Haines
@Chuck, actually, there's a simpler method: NetworkInterface.GetIsNetworkAvailable();
quzui
It doesn't require to loop through adapters ...
quzui