Check for internet connection


///
/// Pings a variation of websites defined in a array and checks for connectivity.
///

/// True if a ping succeeded, False if otherwise.
///
public bool isConnectionAvailable()
{
string[] sitesList = { "www.google.com", "www.microsoft.com" , "www.psychocoder.net" };
Ping ping = new Ping();
PingOptions options = new PingOptions();
PingReply reply;
int notReturned = 0;
try
{
for (int i = 0; i <= sitesList.Length; i++)
{
reply = ping.Send(sitesList[i], 10);
if (reply.Status != IPStatus.Success)
{
notReturned += 1;
}
// Couldn't ping any of the hostnames.
if (notReturned == sitesList.Length)
{
//throw an exception
throw new Exception(@"There doesn't seem to be a network/internet connection.\r\nPlease contact your system administrator");
}
else
{
return true;
}
}
}
catch (Exception ex)
{
return false;
}
}


Comments

Author: Johney Joseph20 Oct 2009 Member Level: Bronze   Points : 1

is this a correct method ? true this may work but there may be apis to check network connection.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: