Software Development Network>> .NET Development>> Internet Connection State
How can i get state of internet connection (Online or offline) with C# Managed Code (No use Win API)
You can use Ping class to ping some Uri like www.google.com.......
I hope it'll help.
Best Regards,
Rizwan aka RizwanSharp
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;
A small modification to the above code to make it perfect, [The code changed in BOLD font]
WebResponse WebRes = WebReq.GetResponse(); if(HttpStatusCode.OK == WebRes.StatusCode)
else
Another Work-around,
System.Net.IPHostEntry objIPHost = System.Net.Dns.GetHostByName("http://www.google.com/");
return true; catch
HTH,
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