I have a problem when trying to detect the computers network connection status. Here is the code I have used:

Dim IsAvalible As String = My.Computer.Network.IsAvailable

If IsAvalible = True Then 
Label1.Text = "Connected"
Else
 If IsAvalible = False Then I
    Label1.Text = "Disconnected"
End If
End If

But for some even when the network is disconnected or the computers wifi is turned on I still get the result of "Connected". I think there something wrong with the code. Could someone help me please ? Thank You

Dani AI

Generated

Short summary and practical fixes for future readers: the original problem is twofold — a small bug in the posted code and a misconception about what a "connected" result means. declared the check as a String and had a malformed If block; use a Boolean and a simple If/Else. Also, being "connected" to a local router or Wi‑Fi access point does not guarantee Internet access — an adapter can be up while the upstream link or DNS/authentication is failing. correctly pointed out the router case; 's simple If pattern is fine once types and logic are fixed.

Recommended troubleshooting steps (in order):

  • Verify the adapter and default gateway are present (ipconfig/Network and Sharing Center).
  • Try reaching the gateway (local ping) to confirm the LAN is OK.
  • Try reaching a known external IP (ICMP to 8.8.8.8) — note ICMP can be blocked.
  • Attempt an HTTP request to a lightweight external URL (HEAD/GET) to detect captive portals or DNS issues.
  • Make the test host and timeouts configurable and perform checks off the UI thread.

Example VB.NET pattern to combine checks (ping first, then a short HTTP HEAD fallback):

Private Function HasInternet(Optional timeoutMs As Integer = 2000) As Boolean
    Try
        Dim p As New System.Net.NetworkInformation.Ping()
        Dim r = p.Send("8.8.8.8", timeoutMs)
        If r.Status = System.Net.NetworkInformation.IPStatus.Success Then Return True
    Catch
        ' continue to HTTP probe
    End Try

    Try
        Dim req = DirectCast(System.Net.WebRequest.Create("http://example.com/"), System.Net.HttpWebRequest)
        req.Method = "HEAD"
        req.Timeout = timeoutMs
        Using resp = req.GetResponse()
            Return True
        End Using
    Catch
        Return False
    End Try
End Function

If HasInternet() Then
    Label1.Text = "Connected"
Else
    Label1.Text = "Disconnected"
End If

Notes and cautions: do these checks asynchronously to avoid freezing the UI; captive portals may return HTTP redirects instead of a clean 200/204, so examine response codes if you need to detect login pages; corporate firewalls may block ICMP or particular hosts, so allow admins to change the probe target.

Recommended Answers

All 2 Replies

If My.Computer.Network.IsAvailable then
         MsgBox("Network detected!")
      Else
         MsgBox("Network connection is not found!")
      End If

Thing is, if you are behind a router then you have always a network available.
try this instead:

Private Declare Function InternetGetConnectedState Lib "wininet.dll" (ByRef lpdwFlags As Int32, ByVal dwReserved As Int32) As Boolean

Public Shared Function IsConnectedToInternet() As Boolean
	Dim Desc As Int32
	Return InternetGetConnectedState(Desc, 0)
End Function

This will return the real status.

commented: + +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.