The following piece of code uses the My.Computer.Network.IsAvailable property to determine whether or not the computer is connected to a network. This network could be the Internet or a local network; for as long as the computer is connected to one such network, the response retrieved will be positive.
Dim networkAvailable As Boolean = My.Computer.Network.IsAvailable
If networkAvailable Then
MsgBox("The network is available")
Else
MsgBox("The network is unavailable")
End If
This Visual Basic (VB.NET) code snippet is used to check if a network connection is available and then display a message box indicating the network status:
- Declaring and Initializing
networkAvailable
:Dim networkAvailable As Boolean = My.Computer.Network.IsAvailable
- This line declares a Boolean variable
networkAvailable
and initializes it with the result ofMy.Computer.Network.IsAvailable
. This property returnsTrue
if a network connection is available, andFalse
otherwise.
- Conditional Check and Message Box Display:
- The
If...Else
statement checks the value ofnetworkAvailable
. - If
networkAvailable
isTrue
(meaning the network is available), it displays a message box with the text “The network is available”. - If
networkAvailable
isFalse
, it shows a different message box stating “The network is unavailable”.
- The
This code is typically used in applications that need to verify network availability before performing network-dependent operations.