Internet Connection State

How can i get state of internet connection (Online or offline) with C# Managed Code (No use Win API)



Answer this question

Internet Connection State

  • Utkarsh Shigihalli

    You can use Ping class to ping some Uri like www.google.com.......

    I hope it'll help.

    Best Regards,

    Rizwan aka RizwanSharp



  • Satya Srikant Mantha

    Here simple workaround method for you

    using System.Net;

    private bool IsInternetConnectionAvailable()

    {

    try

    {

    WebRequest WebReq = WebRequest.Create("http://www.google.com/");

    WebResponse WebRes = WebReq.GetResponse();

    WebRes.Close();

    return true;

    }

    catch

    {

    return false;

    }

    }


  • Basel Al-Khateeb

    A small modification to the above code to make it perfect, [The code changed in BOLD font]

    private bool IsInternetConnectionAvailable()

    {

        try

        {

           WebRequest WebReq = WebRequest.Create("http://www.google.com/");

           WebResponse WebRes = WebReq.GetResponse();
           if(HttpStatusCode.OK == WebRes.StatusCode)

           {

               WebRes.Close();

               return true;            

           }

           else

           {

               WebRes.Close();

               return false;

           }

        catch

        {

           return false;

        }

    }

     

    Another Work-around,

     

    private bool IsInternetConnectionAvailable()

    {

        try

        {

           System.Net.IPHostEntry objIPHost = System.Net.Dns.GetHostByName("http://www.google.com/");

            return true;
        catch

        {

           return false;

        }

    }


    HTH,



  • psi_0

    If you simply want status regarding the addresses assigned to your NICs or if the network cable is plugged in, such events are exposed in the System.Net.NetworkInformation namespace

  • Internet Connection State