///
/// 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;
}
}