Mega Code Archive

 
Categories / C# / Network
 

This method checks if there is a connection at all

using System; using System.Collections.Generic; using System.Linq; using System.Net.NetworkInformation; public static class Net {     private static readonly Ping _ping;     private static readonly List<string> _sites;     static Net()     {         _ping = new Ping();         _sites = new List<string> { "www.google.com", "www.microsoft.com", "www.yahoo.com" };     }     /// <summary>     ///   This method checks if there is a connection at all.     /// </summary>     /// <returns>true if there is a connection, false otherwise</returns>     public static bool IsConnectionAvailable()     {         try         {             var notReturned =                 _sites.Select(site => _ping.Send(site, 10)).Count(reply => reply.Status != IPStatus.Success);             return notReturned != _sites.Count;         }         catch (PingException pingException)         {             Console.WriteLine(pingException.StackTrace);         }         return false;     } }