开发者

Calling C# function on connected to Wifi or Lan

开发者 https://www.devze.com 2023-01-08 05:34 出处:网络
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. Bas

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号