Geeks With Blogs
Pranav`s TechNiche technical thoughts of a coffee addicted developer

Usually, most of the windows applications or other apps which reside on client system, needs to internet connectivity to perform some tasks. If the system is not connected to internet, it will take some time to check for it and return an error. For dealing in this kind of situations, here is a code sample that you can directly use with your code to check whether the system is connected to internet or not. I just found this in Code Project and here is the link for original article.

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

 

Usage:

You just need to call InternetConnectionCheck.IsInternetConnected()

It returns - True, if connected to internet or  False, if not connected to internet.

Posted on Tuesday, April 19, 2011 12:21 PM .NET | Back to top


Comments on this post: Check whether computer is connected to Internet or not - using VB code

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Pranav | Powered by: GeeksWithBlogs.net | Join free