Check if internet connection available
Use this board to post your code snippets - tips and tricks
5 posts
Page 1 of 1
To check internet connection add the following function code:
Code: Select all
Then add the following code where you like to check the connection: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
Code: Select all
If IsConnectionAvailable() = TrueThen
Do Action Here
Else
Do Action Here
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:
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
Or you can just do this
Code: Select all
Just an ideaMe.Text = My.Computer.Network.IsAvailable
- Nocturnal
Nocturnalcodes wrote:Or you can just do thisDepending on how you are going too use the feature.Code: Select allJust an ideaMe.Text = My.Computer.Network.IsAvailable
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
Copyright Information
Copyright © Codenstuff.com 2020 - 2023