Athiwat Chunlakhan
Athiwat Chunlakhan

Reputation: 7799

Calling C# function on connected to Wifi or Lan

I already know how to check if there's a connection to the internet. But I want this(function) to be called when the user plugs a lan line in or connect to a wifi network. Basically am creating a Auto-Login for my university.

Upvotes: 0

Views: 2569

Answers (3)

Yasir Kamal
Yasir Kamal

Reputation: 443

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

Upvotes: 3

Coding Flow
Coding Flow

Reputation: 21881

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.

Upvotes: 2

Alex Krupnov
Alex Krupnov

Reputation: 460

I suppose you can have a listener thread which will simply check status of internet connection and raise event if connection appeared or lost.

Upvotes: 1

Related Questions