ARTICLE
Check Internet Connection
To check for an Internet connection in .NET, we can use GetIsNetworkAvailable method defined in the System.Net.Network-Information namespace.
To check for an Internet connection in .NET, we can use GetIsNetworkAvailable method defined in the System.Net.Network-Information namespace. But it returns a Boolean value that denotes whether any network connection is available and does not say about Internet connectivity. To make sure we also have Internet access, the reliable method is to attempt to ping a host
Imports System.Net.NetworkInformation
Public Function IsConnectedToInternet() As Boolean
Dim host As String = http://www.c-sharpcorner.com
Dim result As Boolean = False
Dim p As Ping = New Ping()
Try
Dim reply As PingReply = p.Send(host, 3000)
If reply.Status = IPStatus.Success Then
Return True
End If
End Try
Return result
End Function
Or we can use a simple API function InternetGetConnectedState. This function takes two arguments:
The first one is an integer used with out keyword, which means that after calling the function, the variable will contain an integer that describes the connection state.
The second one is a reserved variable that must be set to 0.
Imports System
Imports System.Runtime
Imports System.Runtime.InteropServices
Public Class InternetCS
'Creating the extern function...
<DllImport("wininet.dll")> _
private extern static Boolean InternetGetConnectedState( Integer Description, Integer ReservedValue)
'Creating a function that uses the API function...
Public Shared Function IsConnectedToInternet() As Boolean
Dim Desc As Integer
Return InternetGetConnectedState(Desc, 0)
End Function
End Class