1 Replies - 598 Views - Last Post: 04 September 2009 - 04:37 PM

#1 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1663
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Check for internet connection in C#

Posted 26 December 2007 - 03:03 PM

Description: This snippet referers to a seperate object. Theres a referendfce to "ConnectivityNotFoundException", if you want to throw an exception there throw a standard "Exception" as ConnectivityNotFoundException is something from my personal custom Exception library class. I hafve added comments in the snippet to show what to comment and uncomment as you see fit. To use this this just call like shown in the "Example use" part of the snippetThis is a snippet I use to check for an available internet connection , useful when you application requires a connection.
/// 
/// Method used to check for internet connectivity by piging
/// varoaus websites and looking for the response.
/// 
/// True if a ping succeeded, False if otherwise.
/// 
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" };
    //create an instance of the System.Net.NetworkInformation Namespace
    Ping ping = new Ping();
    //Create an instance of the PingReply object from the same Namespace
    PingReply reply;
    //int variable to hold # of pings not successful
    int notReturned = 0;
     try
        {
         //start a loop that is the lentgh of th string array we
         //created above
            for (int i = 0; i <= sitesList.Length; i++)
            {
                //use the Send Method of the Ping object to send the
                //Ping request
                reply = ping.Send(sitesList[i], 10);
                //now we check the status, looking for,
                //of course a Success status
                if (reply.Status != IPStatus.Success)
                {
                    //now valid ping so increment
                    notReturned += 1;
                }
                //check to see if any pings came back
                if (notReturned == sitesList.Length)
                {
                    _success = false;
                    //comment this back in if you have your own excerption
                    //library you use for you applications (use you own
                    //exception names)
                    //throw new ConnectivityNotFoundException(@"There doest seem to be a network/internet connection.rn 
                     //Please contact your system administrator");
                    //use this is if you don't your own custom exception library
                    throw new Exception(@"There doest seem to be a network/internet connection.rn 
                    Please contact your system administrator");
                }
                else
                {
                    _success = true;
                }
            } 
    }
    //comment this back in if you have your own excerption
    //library you use for you applications (use you own
    //exception names)
    //catch (ConnectivityNotFoundException ex)
    //use this line if you don't have your own custom exception 
    //library
    catch (Exception ex)
    {
        _success = false;
        _returnMessage = ex.Message;
    }
    return _success;
}

//Example Useage
If(!(isConnectionAvailable))
{
   //then do something
} 
{
   //then do something
} 


Is This A Good Question/Topic? 0
  • +

Replies To: Check for internet connection in C#

#2 prince11   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 12-August 09

Re: Check for internet connection in C#

Posted 04 September 2009 - 04:37 PM

it is very helpful
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1