SIGN UP MEMBER LOGIN:    
ARTICLE

Checking if User is connected to Internet or Not using Win32 API and C#

Posted by Kirtan Patel Articles | Learn .NET August 06, 2010
Article Checking if User is connected to Internet or Not using Win32 API and C#
Reader Level:
Download Files:
 

In application like Messenger or Application that provides automatic update need to check that user is connected to internet or not before proceeding further.

so we need to check whether  he is connected to internet or not we can check that with use of WIN32 API.

we can use Win32 API Wininet.dll 'sInternetGetConnectedState() method to check Internet Status 

for working with Pinvoke we need to add name space 

using System.Runtime.InteropServices;

now we need to write prototyping of the function  like below

[DllImport("wininet.dll")]

private extern static bool InternetGetConnectedState(outint connectionDescription, int reservedValue);
 

Now simply we can use this function to check internet connectivity 
 

take one button and label on form In buttons's click even write following code 

       

private void btnCheckConnection_Click(objectsender, EventArgs e)

        {

            int Description=0;

            bool isConnected = InternetGetConnectedState(out Description, 0);

            if (isConnected == true)

            {

                label1.Text = "User is Connected to Internet ";

            }

            else

            {

                label1.Text = "Disconnected";

            }

        }

8-7-2010 9-10-40 AM.gif

Here is how complete code look like 

-----------------------------------------------

 

public partial class Form1 : Form

    {

        [DllImport("wininet.dll")]

        private extern static bool InternetGetConnectedState(out int connectionDescription,int reservedValue);

       

        public Form1()

        {

            InitializeComponent();

        }

  

        private void btnCheckConnection_Click(object sender, EventArgs e)

        {

            int Description=0;

            bool isConnected = InternetGetConnectedState(out Description, 0);

            if (isConnected == true)

            {

                label1.Text = "User is Connected to Internet ";

            }

            else

            {

                label1.Text = "Disconnected";

            }

        }

    }

Thank you.

Login to add your contents and source code to this article
share this article :
post comment
 

its checking for all kind of internet connectivity not lan Only .
i don't use Ethernet card i am using EVDO Connection still code shows status perfectly even it works with wifi too ..

Posted by Kirtan Patel Aug 18, 2010

Hi Buddy,


This code only check LAN avail or not , not an internet

Thanks, Sankar B

Posted by sankar narayanan Aug 18, 2010
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor