SIGN UP MEMBER LOGIN:    
ARTICLE

Check Internet Connection

Posted by Nipun Tomar Articles | Internet & Web January 29, 2009
To check for an Internet connection in .NET, we can use GetIsNetworkAvailable method defined in the System.Net.Network-Information namespace.
Reader Level:

To check for an Internet connection in .NET, we can use GetIsNetworkAvailable method defined in the System.Net.Network-Information namespace. But it returns a Boolean value that denotes whether any network connection is available and does not say about Internet connectivity. To make sure we also have Internet access, the reliable method is to attempt to ping a host

...
using System.Net.NetworkInformation;
...
public bool IsConnectedToInternet()
        {
            string host = http://www.c-sharpcorner.com;
            bool result = false;
            Ping p = new Ping();
            try
            {
                PingReply reply = p.Send(host, 3000);
                if (reply.Status == IPStatus.Success)
                    return true;
            }
            catch { }
            return result;
        }

Or we can use a simple API function InternetGetConnectedState. This function takes two arguments:

The first one is an integer used with out keyword, which means that after calling the function, the variable will contain an integer that describes the connection state.

The second one is a reserved variable that must be set to 0.

using System ;
using System.Runtime ;
using System.Runtime.InteropServices ;
public class InternetCS
{
//Creating the extern function...
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState( out int Description, int ReservedValue ) ;
//Creating a function that uses the API function...
public static bool IsConnectedToInternet( )
{
int Desc ;
return InternetGetConnectedState( out Desc, 0 ) ;
}
}

Login to add your contents and source code to this article
share this article :
post comment
 

put the code in the form where you want to check the internet connection.

Posted by Nipun Tomar Feb 19, 2009

hi i want to ask where should i put this code? nd i want to display my own error message.

Posted by shweta parikh Feb 16, 2009

probably ICMP (internet control message protocol) is blocked by google.

Posted by Nipun Tomar Feb 11, 2009

string host = http://www.c-sharpcorner.com should be string host = "http://www.c-sharpcorner.com", I test this URL is successful! but www.google.com is false, Do u know reason?

Posted by wrlei Feb 11, 2009
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Team Foundation Server Hosting
Become a Sponsor