# VBForums CodeBank > CodeBank - C# >  Detecting Internet Connections from Your C# Code.

## firoz.raj

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;*


```
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;
                
            }
        }
    }
}
```

----------


## firoz.raj

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 .

```
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);

            }

        }



    }
}
```

----------


## firoz.raj

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 .

```
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;
            }
        }
    }
}
```

----------

