This method checks if there is a connection at all : Ping « Network « C# / C Sharp






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

   
    
    
    
    
    
    
    
  








Related examples in the same category

1.Success
2.Ping and PingReply
3.Ping Success and Send
4.Advanced Ping ProgramAdvanced Ping Program
5.Simple Ping
6.Ping a host
7.Uses the System.Net.NetworkInformation.Ping class to send an ICMP ping to a specified host asynchronously.
8.Is Internet Connection Available