Check if internet connection available

Use this board to post your code snippets - tips and tricks
5 posts Page 1 of 1
Contributors
MANSQN
VIP - Donator
VIP - Donator
Posts: 159
Joined: Sat Sep 17, 2011 11:33 pm

To check internet connection add the following function code:
Code: Select all
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:
Code: Select all
If IsConnectionAvailable() = TrueThen

Do Action Here

Else

Do Action Here
User avatar
Code
VIP - Donator
VIP - Donator
Posts: 51
Joined: Sun Sep 18, 2011 7:25 pm

If you would like too improve this, try adding 1 label, change its text too network status: then add a second label and leave it blank but put it next too the label1.

Double click on your form and add this code:
Code: Select all
Public Class Form1
    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

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If IsConnectionAvailable() = True Then

            Label2.Text = "ONLINE"
            Label2.BackColor = Color.Green

        Else

            Label2.Text = "OFFLINE"
            Label2.BackColor = Color.Red
        End If
    End Sub
End Class
viewtopic.php?f=17&p=54101#p54101
Image
Image
Image
User avatar
Nocturnalcodes
Dedicated Member
Dedicated Member
Posts: 51
Joined: Tue Aug 16, 2011 12:08 am

Or you can just do this
Code: Select all
Me.Text = My.Computer.Network.IsAvailable
Just an idea
- Nocturnal
User avatar
Code
VIP - Donator
VIP - Donator
Posts: 51
Joined: Sun Sep 18, 2011 7:25 pm

Nocturnalcodes wrote:
Or you can just do this
Code: Select all
Me.Text = My.Computer.Network.IsAvailable
Just an idea
Depending on how you are going too use the feature.
viewtopic.php?f=17&p=54101#p54101
Image
Image
Image
User avatar
Danny
VIP - Donator
VIP - Donator
Posts: 621
Joined: Sat Oct 30, 2010 8:21 pm

the network is available code isn't handy when you use wireless internet connection, sometimes it says you are connected when you are not connected
5 posts Page 1 of 1
Return to “Quick Snips”