Check for internet connection in C#
public bool isConnectionAvailable()
{
//build a list of sites to ping, you can use your own
string[] sitesList = { "www.google.com", "www.microsoft.com" , "www.psychocoder.net" };
Ping ping = new Ping();
PingReply reply;
bool _success = false;
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;
}
if (notReturned == sitesList.Length)
{
_success = false;
throw new Exception(@"There doest seem to be a network/internet connection.\r\n
Please contact your system administrator");
}
else
{
_success = true;
}
}
}
catch
{
_success = false;
}
return _success;
}
منبع : http://www.dreamincode.net/code/snippet1568.htm