ARTICLE

Check Internet Connection

Posted by Nipun Tomar Articles | Web Dev in VB.NET January 30, 2009
To check for an Internet connection in .NET, we can use GetIsNetworkAvailable method defined in the System.Net.Network-Information namespace.
 
Reader Level:

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

Login to add your contents and source code to this article
share this article :
post comment
 

put the code in the form where you want to check the internet connection.

Posted by Nipun Tomar Feb 19, 2009

hi i want to ask where should i put this code? nd i want to display my own error message.

Posted by shweta parikh Feb 16, 2009

probably ICMP (internet control message protocol) is blocked by google.

Posted by Nipun Tomar Feb 11, 2009

string host = http://www.c-sharpcorner.com should be string host = "http://www.c-sharpcorner.com", I test this URL is successful! but www.google.com is false, Do u know reason?

Posted by wrlei Feb 11, 2009
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor