Home > DeveloperSection > Forums > how to check internet connection is available in C#?
lois waisbrooker
lois waisbrooker

Total Post:70

Points:494
Posted on    June-08-2013 7:41 AM

 C# C# 
Ratings:


 1 Reply(s)
 1646  View(s)
Rate this:
Hi Mindstickians,

I'm developing one windows application, where we perform some task related to internet connection. 
So please tell me how to check internet connection is available or not in C#.

Thanks in advance. 


Vijay Shukla

Total Post:100

Points:700
Posted on    June-08-2013 7:59 AM

Hello lois waisbrooker !
Copy this code snippets and paste where which you want, This method return a bool type value and its parameter get a long value for minimumSpeed of your internet.

public static bool IsNetworkAvailable(long minimumSpeed)
        {
            if (!NetworkInterface.GetIsNetworkAvailable())
                return false;

            foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
            {
                // discard because of standard reasons
                if ((ni.OperationalStatus != OperationalStatus.Up) ||
                    (ni.NetworkInterfaceType == NetworkInterfaceType.Loopback) ||
                    (ni.NetworkInterfaceType == NetworkInterfaceType.Tunnel))
                    continue;

                // this allow to filter modems, serial, etc.
                // I use 10000000 as a minimum speed for most cases
                if (ni.Speed < minimumSpeed)
                    continue;

                // discard virtual cards (virtual box, virtual pc, etc.)
                if ((ni.Description.IndexOf("virtual", StringComparison.OrdinalIgnoreCase) >= 0) ||
                    (ni.Name.IndexOf("virtual", StringComparison.OrdinalIgnoreCase) >= 0))
                    continue;

                // discard "Microsoft Loopback Adapter", it will not show as NetworkInterfaceType.Loopback but as Ethernet Card.
                if (ni.Description.Equals("Microsoft Loopback Adapter", StringComparison.OrdinalIgnoreCase))
                    continue;

                return true;
            }
            return false;
        }

Don't want to miss updates? Please click the below button!

Follow MindStick