Click here to Skip to main content
15,881,809 members
Articles / Web Development / ASP.NET
Tip/Trick

Checking Internet Is Connected Or Not From .NET

Rate me:
Please Sign up or sign in to vote.
4.60/5 (41 votes)
25 Dec 2011CPOL2 min read 56.9K   47   8
Checking Internet Is Connected Or Not From .NET

Introduction



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.

Background



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
        INTERNET_CONNECTION_MODEM = &H1
        INTERNET_CONNECTION_LAN = &H2
        INTERNET_CONNECTION_PROXY = &H4
        INTERNET_RAS_INSTALLED = &H10
        INTERNET_CONNECTION_OFFLINE = &H20
        INTERNET_CONNECTION_CONFIGURED = &H40
    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
        Try
            If InternetGetConnectedState(flags, 0) Then
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            Throw ex
        Finally
            flags = Nothing
        End Try
    End Function

End Class


History



About the Author
Gandhi Ashish

License

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


Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Christian Amado1-Aug-12 7:34
professionalChristian Amado1-Aug-12 7:34 
SuggestionChange your tags. Pin
StianSandberg12-Jul-12 2:53
StianSandberg12-Jul-12 2:53 
Question"the" internet Pin
Henning Dieterichs7-Apr-12 0:00
Henning Dieterichs7-Apr-12 0:00 
Question[My vote of 2] Other way is to use command line Pin
zenwalker198526-Mar-12 5:00
zenwalker198526-Mar-12 5:00 
GeneralInternetGetConnectedState is not reliable api! as mentioned ... Pin
ThatsAlok27-Dec-11 2:14
ThatsAlok27-Dec-11 2:14 
GeneralReason for my vote of 1 This returns True if connected to a ... Pin
Paul_Williams7-Dec-11 1:35
Paul_Williams7-Dec-11 1:35 
GeneralReason for my vote of 5 Nice explanation.... Pin
Pravin Patil, Mumbai7-Oct-11 8:30
Pravin Patil, Mumbai7-Oct-11 8:30 
Generalgood Article Pin
Hemant_ec484-Jan-10 6:28
Hemant_ec484-Jan-10 6: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.