Introduction
In some scenarios you need to check whether internet is available or not using C# code in windows applications. May be to download or upload a file using internet in windows forms or to get some data from database which is at remote location, in these situations internet check is compulsory.
There are some ways to check internet availability using C# from code behind. All such ways are explained here including their limitations.
1. InternetGetConnectedState(wininet)
The 'wininet' API can be used to check the local system has active internet connection or not. The namespace used for this is 'System.Runtime.InteropServices' and import the dll 'wininet.dll' using DllImport. After this create a boolean variable with extern static with a function name InternetGetConnectedState with two parameters description and reservedValue as shown in example.
Note: The extern modifier is used to declare a method that is implemented externally. A common use of the extern modifier is with the DllImport attribute when you are using Interop services to call into unmanaged code. In this case, the method must also be declared as static.
Next create a method with name 'IsInternetAvailable' as boolean. The above function will be used in this method which returns internet status of local system
[DllImport("wininet.dll")] private extern static bool InternetGetConnectedState(out int description, int reservedValue); public static bool IsInternetAvailable() { try { int description; return InternetGetConnectedState(out description, 0); } catch (Exception ex) { return false; } }
2. GetIsNetworkAvailable
The following example uses the GetIsNetworkAvailable method to determine if a network connection is available.
if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()) { System.Windows.MessageBox.Show("This computer is connected to the internet"); } else { System.Windows.MessageBox.Show("This computer is not connected to the internet"); }
Remarks (As per MSDN): A network connection is considered to be available if any network interface is marked "up" and is not a loopback or tunnel interface.
There are many cases in which a device or computer is not connected to a useful network but is still considered available and GetIsNetworkAvailable will return true. For example, if the device running the application is connected to a wireless network that requires a proxy, but the proxy is not set, GetIsNetworkAvailable will return true. Another example of when GetIsNetworkAvailable will return true is if the application is running on a computer that is connected to a hub or router where the hub or router has lost the upstream connection.
3. Ping a hostname on the network
Ping and PingReply classes allows an application to determine whether a remote computer is accessible over the network by getting reply from the host. These classes are available in System.Net.NetworkInformation namespace. The following example shows how to ping a host.
protected bool CheckConnectivity(string ipAddress) { bool connectionExists = false; try { System.Net.NetworkInformation.Ping pingSender = new System.Net.NetworkInformation.Ping(); System.Net.NetworkInformation.PingOptions options = new System.Net.NetworkInformation.PingOptions(); options.DontFragment = true; if (!string.IsNullOrEmpty(ipAddress)) { System.Net.NetworkInformation.PingReply reply = pingSender.Send(ipAddress); connectionExists = reply.Status == System.Net.NetworkInformation.IPStatus.Success ? true : false; } } catch (PingException ex) { Logger.LogException(ex.Message, ex); } return connectionExists; }
Remarks (As per MSDN): Applications use the Ping class to detect whether a remote computer is reachable. Network topology can determine whether Ping can successfully contact a remote host. The presence and configuration of proxies, network address translation (NAT) equipment, or firewalls can prevent Ping from succeeding. A successful Ping indicates only that the remote host can be reached on the network; the presence of higher level services (such as a Web server) on the remote host is not guaranteed.
Comments/Suggestions are invited. Happy coding......!
Comments Post a Comment