20 Mar

Method to check internet access in csharp

In this post we will know the method to check internet access in csharp.

Usage

Mostly used for sites that used to display data in the offline mode.
If we have an internet connection, download data and display from the webservice services, otherwise check the cache
and see if we have the data available

Implementation

/// 
/// External method for checking internet access
/// 
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState(out int Description, int ReservedValue);


// 
//C# method to check the internet access
//
public static bool IsConnectedToInternet()
{
	int Description;
	return InternetGetConnectedState(out Description, 0);
}

 public ActionResult Index()
 {         
     if (IsConnectedToInternet())
      {

      }
    else
   {
     //Display data from the cache 
   }
            return View();
  }

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>