-
Oct 11th, 2013, 05:54 AM
#1
Thread Starter
Frenzied Member
Detecting Internet Connections from Your C# Code.
Hi,Knowing whether or not Pc Running and it is connected to internet or not . this article will demostrate the simple and
easy 3 way to connect with internet .today i simple demostrate one method .let us start .and please don't forget to import the following Namespace.
Using System.net.Socket;
Code:
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.Sockets;
namespace PrjCheckInternetCon
{
public partial class Form1 : Form
{
public Form1() {
InitializeComponent();
}
private void btCheckInternetCon_Click(object sender, EventArgs e) {
if (checkInternetCon()){
MessageBox.Show("You are Connected to Internet");
}
}
public bool checkInternetCon() {
try {
TcpClient Tcp = new TcpClient("WWW.GMAIL.COM", 80);
Tcp.Close();
return true;
}
catch {
return false;
}
}
}
}
-
Oct 15th, 2013, 10:36 AM
#2
Thread Starter
Frenzied Member
Re: Detecting Internet Connections from Your C# Code.
Hi,Friend today i have been posting my 2nd way to check internet connection . please don't forget to import
System.Net .i have been using here ipHostEntry object of System.net .
Code:
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;
namespace ConnectInternet
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public bool Connected(string URL) {
try {
IPHostEntry hostentry = Dns.GetHostEntry(URL);
return true;
}
catch{
MessageBox.Show("Sorry,Internet is not connected", "Internet N/A", MessageBoxButtons.OK);
return false;
}
}
private void btcheckInternet_Click(object sender, EventArgs e){
if (Connected("WWW.GMAIL.COM")) {
MessageBox.Show("Internet is connected", "Internet Available", MessageBoxButtons.OK);
}
}
}
}
Last edited by firoz.raj; Oct 15th, 2013 at 01:02 PM.
-
Oct 15th, 2013, 09:20 PM
#3
Thread Starter
Frenzied Member
Re: Detecting Internet Connections from Your C# Code.
Hi Friends, last but not a least please don't forget to write DLLImport["Wininet.dll"] attribute .following example is simplified way to check internet connectivity .
Code:
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.Runtime.InteropServices;
namespace CONNECTIVITY{
public partial class frmInternetcheck2 : Form
{
[DllImport("wininet.dll")]
private extern static bool internetgetConnectionState(out int description, int reservedValue);
public frmInternetcheck2()
{
InitializeComponent();
}
private void btcheckinternet_Click(object sender, EventArgs e) {
if (checkInternetConnection()) {
MessageBox.Show("Connected");
}
else {
MessageBox.Show("Not Connected");
}
}
public static bool checkInternetConnection() {
try {
int condesc;
return internetgetConnectionState(out condesc, 0);
}
catch {
return false;
}
}
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|