Files
2025-04-28 12:51:07 -04:00

75 lines
2.5 KiB
C#
Executable File

using System.Net.NetworkInformation;
namespace MarketData.Integration
{
public class NetworkStatus
{
private NetworkStatus()
{
}
// On Linux platforms we must be priveleged to send custom payloads so we are using the send option that does not require a payload
// This differs from the windows implementation
public static bool IsInternetConnected()
{
try
{
Ping myPing = new Ping();
String host = "google.com";
int timeout = 5000;
PingReply reply = myPing.Send(host, timeout);
if (reply.Status == IPStatus.Success)
{
return true;
}
else if (reply.Status == IPStatus.TimedOut)
{
return true;
}
else
{
return false;
}
}
catch (Exception exception)
{
MDTrace.WriteLine(LogLevel.DEBUG,$"[Ping] {exception.ToString()}");
return false;
}
}
/// <summary>
/// Indicates whether any network connection is available.
/// Filter connections below a specified speed, as well as virtual network cards.
/// </summary>
/// <returns>
/// <c>true</c> if a network connection is available; otherwise, <c>false</c>.
/// </returns>
public static bool IsNetworkAvailable()
{
if (!NetworkInterface.GetIsNetworkAvailable())
return false;
foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
{
// discard because of standard reasons
if ((ni.OperationalStatus != OperationalStatus.Up) ||
(ni.NetworkInterfaceType == NetworkInterfaceType.Loopback) ||
(ni.NetworkInterfaceType == NetworkInterfaceType.Tunnel))
continue;
// discard virtual cards (virtual box, virtual pc, etc.)
if ((ni.Description.IndexOf("virtual", StringComparison.OrdinalIgnoreCase) >= 0) ||
(ni.Name.IndexOf("virtual", StringComparison.OrdinalIgnoreCase) >= 0))
continue;
// discard "Microsoft Loopback Adapter", it will not show as NetworkInterfaceType.Loopback but as Ethernet Card.
if (ni.Description.Equals("Microsoft Loopback Adapter", StringComparison.OrdinalIgnoreCase))
continue;
return true;
}
return false;
}
}
}