Check if internet connection available in Visual Basic .NET

Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013

To check internet connection add the following function code:
Public Function IsConnectionAvailable() As Boolean
        Dim objUrl As New System.Uri("http://www.youtube.com")
        Dim objWebReq As System.Net.WebRequest
        objWebReq = System.Net.WebRequest.Create(objUrl)
        Dim objresp As System.Net.WebResponse

        Try
            objresp = objWebReq.GetResponse
            objresp.Close()
            objresp = Nothing
            Return True

        Catch ex As Exception
            objresp = Nothing
            objWebReq = Nothing
            Return False
        End Try
End Function
Then add the following code where you like to check the connection:

If IsConnectionAvailable() = TrueThen
Do Action Here
Else
Do Action Here

End If


Share This

Home | About | Contact | Privacy Policy

Copyright visual-basic-tutorials.com 2017 - All Rights Reserved.