01-20-2012، 12:49 PM
Check if an Internet Connection is available
کد پیاچپی:
''' <summary>
''' Pings a variation of websites defined in a array and checks for connectivity.
''' </summary>
''' <returns>True if a ping succeeded, False if otherwise.</returns>
''' <remarks></remarks>
Public Function isInternetAvailable() As Boolean
' The arrays of sites we want to check for connectivity.
' If all fail to ping, the function will return false.
Dim sitesToCheck() As String = {"www.google.com", _
"www.microsoft.com", "www.dreamincode.net"}
Dim failedPings As Integer = 0
' Loop through all hostnames and ping them
For Each str As String In sitesToCheck
Try
My.Computer.Network.Ping(str)
Catch ex As Exception
failedPings += 1
End Try
Next
' Couldn't ping any of the hostnames.
If failedPings = sitesToCheck.Length Then Return False
Return True
End Function
' Example Usage
MessageBox.Show(isInternetAvailable())