Monday, June 14, 2010

Checking net connection in your system in C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.NetworkInformation;

namespace demoConnection
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

}

private void timer1_Tick(object sender, EventArgs e)
{
bool test = IsConnectedToInternet();
if (test == true)
{
label1.Text = " Connected";
}
else
{
label1.Text = "Not Connected";
}
}

private bool IsConnectedToInternet()
{
string host = "http://www.c-sharpcorner.com";
bool result = false;
Ping p = new Ping();
try
{
PingReply reply = p.Send(host, 3000);
if (reply.Status == IPStatus.Success)
return true;
}
catch { }
return result;
}
}
}