Click here to Skip to main content
Click here to Skip to main content

Checking Internet Is Connected Or Not From .NET

By , 25 Dec 2011
Rate this:
Please Sign up or sign in to vote.

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)

About the Author

Gandhi Ashish
Software Developer
India India
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 PinmemberChristian Amado1-Aug-12 7:34 
SuggestionChange your tags. Pinmemberstian.net12-Jul-12 2:53 
Question"the" internet PinmemberHenning Dieterichs7-Apr-12 0:00 
Question[My vote of 2] Other way is to use command line Pinmemberzenwalker198526-Mar-12 5:00 
GeneralInternetGetConnectedState is not reliable api! as mentioned ... PinmemberThatsAlok27-Dec-11 2:14 
GeneralReason for my vote of 1 This returns True if connected to a ... PingroupPaul Pencoed7-Dec-11 1:35 
GeneralReason for my vote of 5 Nice explanation.... PinmemberPravin Patil, Mumbai7-Oct-11 8:30 
Generalgood Article PinmemberHemant_ec484-Jan-10 6:28 

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

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

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