C# - Check for Internet connection

By , 7/30/2013
(1 ratings)
This method checks (using Ping) whether a connection to the Internet is available or not.

Following namespace is needed:
using System.Net.NetworkInformation;


Author: Günther Foidl, translation by Michael List
/// <summary>
/// Checks whether an Internet connection is available
/// </summary>
/// <returns>
/// true/false
/// </returns>
public static bool CheckInternetConnection()
{
	Ping ping = new Ping();

	try
	{
		PingReply reply = ping.Send("www.google.com", 100);

		return reply.Status == IPStatus.Success;
	}
	catch
	{
		return false;
	}
}

Comments

 

Log in, to comment!