How to get current connection state ? (network cable plugged or not ?)
I need 开发者_如何转开发to get current connection state.
I found: System.Net.NetworkInformation.NetworkChange.NetworkAvailabilityChanged event but how can I get current network state ?
You are probably looking for the NetworkInterface
class.
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);
}
}
}
精彩评论