Saturday, March 7, 2009

How can we check that our internet connection is working or not.

private void Form1_Load(object sender, EventArgs e)
{
try
{
this.testconnection();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

private void testconnection()
{
if (this.IsConnectionAvailable())
MessageBox.Show("Internet available");
else
MessageBox.Show("No Internet");
}

public bool IsConnectionAvailable()
{
System.Net.WebRequest objWebReq = System.Net.WebRequest.Create("http://www.microsoft.com/");
System.Net.WebResponse objResp = default(System.Net.WebResponse);
try
{
objResp = objWebReq.GetResponse();
objResp.Close();
objWebReq = null;
IsConnected = true;
return true;
}

catch (Exception ex)
{
objResp = null;
objWebReq = null;
IsConnected = false;
return false;
}
}

Courtesy: http://www.devasp.net/net/articles/display/639.html
-----------------------------------------------------------------------------------
private bool CheckInternetAvailability()
{
try
{
System.Net.IPHostEntry ipaddress = System.Net.Dns.GetHostByName("www.google.com");
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return false;
}
}

Courtesy: http://www.csharpfriends.com/Forums/ShowPost.aspx?PostID=13045

No comments:

Post a Comment