954,557 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Detecting network status problem

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 <code> &lt;code&gt;If IsAvalible = False Then&lt;/code&gt; I &lt;code&gt; Label1.Text = &amp;quot;Disconnected&amp;quot;&lt;/code&gt;</code> End If [/ICODE] End If[/ICODE]

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

PS. Im not sure how to use the ICODE Tagging thing, sorry

VB_Magic
Newbie Poster
1 post since Aug 2011
Reputation Points: 10
Solved Threads: 0
 
If My.Computer.Network.IsAvailable then
         MsgBox("Network detected!")
      Else
         MsgBox("Network connection is not found!")
      End If
Netcode
Veteran Poster
1,021 posts since Jun 2009
Reputation Points: 43
Solved Threads: 67
 

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.

GeekByChoiCe
Master Poster
721 posts since Jun 2009
Reputation Points: 208
Solved Threads: 168
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: