Monday, 28 January 2013

Check internet connectivity is available/connected in C#



public static bool CheckInternetConnectivity()
{
bool RetVal = false;
       System.Net.WebRequest WebReq;
       try
       {
              System.Uri Url = new System.Uri("http://www.google.com");

              System.Net.WebResponse Resp;
              WebReq = System.Net.WebRequest.Create(Url);

              Resp = WebReq.GetResponse();
              Resp.Close();
              WebReq = null;
              RetVal = true;
}
       catch
       {
              WebReq = null;
              RetVal = false; // host not reachable.
}

       return RetVal;
}

No comments:

Post a Comment