Imports System.Net.NetworkInformation
Namespace ElektroKit.Core.NET.Tools
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Contains networking related utilities.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
Public NotInheritable Class NetworkUtil
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Prevents a default instance of the <see cref="NetworkUtil"/> class from being created.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
<DebuggerNonUserCode>
Private Sub New()
End Sub
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Gets a value indicating whether at least one of the current network adapters is capable of connecting to Internet.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
''' <value>
''' <see langword="True"/> if at least one of the current network adapters is capable of connecting to Internet;
''' otherwise, <see langword="False"/>.
''' </value>
''' ----------------------------------------------------------------------------------------------------
Public Shared ReadOnly Property IsNetworkAvailable As Boolean
<DebuggerStepThrough>
Get
Return NetworkUtil.GetNetworkAvailable()
End Get
End Property
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Evaluate the online network adapters to determine if at least one of them is capable of connecting to Internet.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
''' <returns>
''' <see langword="True"/> if at least one of the current network adapters is capable of connecting to Internet;
''' otherwise, <see langword="False"/>.
''' </returns>
''' ----------------------------------------------------------------------------------------------------
<DebuggerStepThrough>
Private Shared Function GetNetworkAvailable() As Boolean
' Only recognizes changes related to Internet adapters.
If NetworkInterface.GetIsNetworkAvailable() Then
' However, this will include all adapters.
Dim interfaces As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
For Each ni As NetworkInterface In interfaces
' Filter so we see only Internet adapters.
If ni.OperationalStatus = OperationalStatus.Up Then
If (ni.NetworkInterfaceType <> NetworkInterfaceType.Tunnel) AndAlso
(ni.NetworkInterfaceType <> NetworkInterfaceType.Loopback) Then
Dim statistics As IPv4InterfaceStatistics = ni.GetIPv4Statistics()
' All testing seems to prove that once an interface comes online
' it has already accrued statistics for both received and sent...
If (statistics.BytesReceived > 0) AndAlso (statistics.BytesSent > 0) Then
Return True
End If
End If
End If
Next ni
End If
Return False
End Function
End Class
End Namespace