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
    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



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: