C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Interview   Jobs   Projects   Offshore Development    
Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing | Talk to Us |



My Profile

Gifts

Active Members
TodayLast 7 Days more...









Check Internet connection present or not


Posted Date: 09 May 2008    Resource Type: Code Snippets    Category: C# Syntax

Posted By: narasimha       Member Level: Gold
Rating:     Points: 10



///
/// 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" };
//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.\r\n
//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.\r\n
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
}




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: How to Eject CD tray using Win32 API
Previous Resource: How to determine a session expired or not
Return to Discussion Resource Index
Post New Resource
Category: C# Syntax


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

fax server

Contact Us    Privacy Policy    Terms Of Use