Click here to Skip to main content
13,350,289 members (47,196 online)
Click here to Skip to main content
Add your own
alternative version


47 bookmarked
Posted 4 Jan 2010

Checking Internet Is Connected Or Not From .NET

, 25 Dec 2011
Rate this:
Please Sign up or sign in to vote.
Checking Internet Is Connected Or Not From .NET


Hi!! Thanks to all readers for visiting this post. This post could be useful for those who need to check the internet connection before performing some task in their application.


I was working on one project in which I was making a communication to the server each and every time for performing some of the tasks needed by the application. This communication however I was doing with Web service. I took the reference of this web service from my Windows application and was calling the web methods to perform different tasks. However, this application was like a Windows service which automatically needed to make the log on time entry to the server using web method. For this, whenever windows starts, I had to auto login to the server using the internet connection,and for doing so, I needed to check whether the internet is connected over client machine or not, which lead me to find some solution in that direction.

Using the code

The code is so simple. Just Copy & Paste the class below in your application, and call the function IsInternetConnected() to determine whether the internet is connected or not. Let me explain what actually I am doing here. Here I am referring to an external Windows API function using the DllImport. Later on, from my function IsInternetConnected(), I am calling that function with sufficient parameters. As the functions return type is boolean, it returns to me whether the internet connected or not. That's all.

However, as this is my first article on CodeProject, maybe my language could be undescriptive. So please forgive me for that. But the code below is easily understandable by any programmer, so I don't think any further explanation is needed for that. With the hope that this article would be useful to my friends, I have put it here.

Imports System
Imports System.Runtime.InteropServices
Imports System.Text

''' <summary>
''' Determine whether or not there is a connection to the Internet present on the local machine.
''' </summary>
''' <remarks></remarks>
Public Class InternetConnectionCheck

    <DllImport("WININET", CharSet:=CharSet.Auto)> _
    Private Shared Function InternetGetConnectedState_
(ByRef lpdwFlags As InternetConnectionState, ByVal dwReserved As Integer) As Boolean
    End Function

    <Flags()> _
    Public Enum InternetConnectionState As Integer
    End Enum

    ''' <summary>
    ''' Call this function to know whether the internet is connected or not.
    ''' </summary>
    ''' <returns>Boolean</returns>
    ''' <remarks></remarks>
    Public Shared Function IsInternetConnected() As Boolean
        Dim flags As InternetConnectionState = 0
            If InternetGetConnectedState(flags, 0) Then
                Return True
                Return False
            End If
        Catch ex As Exception
            Throw ex
            flags = Nothing
        End Try
    End Function

End Class


About the Author
Gandhi Ashish


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Gandhi Ashish
Software Developer
India India
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Christian Amado1-Aug-12 8:34
memberChristian Amado1-Aug-12 8:34 
SuggestionChange your tags. Pin
stian.net12-Jul-12 3:53
memberstian.net12-Jul-12 3:53 
Question"the" internet Pin
Henning Dieterichs7-Apr-12 1:00
memberHenning Dieterichs7-Apr-12 1:00 
Question[My vote of 2] Other way is to use command line Pin
zenwalker198526-Mar-12 6:00
memberzenwalker198526-Mar-12 6:00 
GeneralInternetGetConnectedState is not reliable api! as mentioned ... Pin
ThatsAlok27-Dec-11 3:14
memberThatsAlok27-Dec-11 3:14 
GeneralReason for my vote of 1 This returns True if connected to a ... Pin
Paul Pencoed7-Dec-11 2:35
groupPaul Pencoed7-Dec-11 2:35 
GeneralReason for my vote of 5 Nice explanation.... Pin
Pravin Patil, Mumbai7-Oct-11 9:30
memberPravin Patil, Mumbai7-Oct-11 9:30 
Generalgood Article Pin
Hemant_ec484-Jan-10 7:28
memberHemant_ec484-Jan-10 7:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 25 Dec 2011
Article Copyright 2010 by Gandhi Ashish
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid