I already know how to check if there's a connection to the internet. But I want this(function) to be开发者_StackOverflow社区 called when the user plugs a lan line in or connect to a wifi network. Basically am creating a Auto-Login for my university.
NetworkInterface[] nics=NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in nics)
{
if (adapter.OperationalStatus == OperationalStatus.Up)
{
if (adapter.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
{
Console.WriteLine("Wifi");
}
}
}
NetworkInterfaceType.Lan works for LAN
By far the easiest method is to simply attempt to periodically run the standard auto login procedure, if it works all is well and the user is logged in if it does not then there is no connection. There is little point trying to determine WHY there is no connection unless you are writitng some kind network connectivity diagnostic app.
I suppose you can have a listener thread which will simply check status of internet connection and raise event if connection appeared or lost.
精彩评论