Mercurial > servermonitor
diff ServerMonitor/Helpers.cs @ 4:3142e52cbe69
Lots more progress
author | Brad Greco <brad@bgreco.net> |
---|---|
date | Sun, 10 Feb 2019 20:51:26 -0500 |
parents | 453ecc1ed9ea |
children | b6fe203af9d5 |
line wrap: on
line diff
--- a/ServerMonitor/Helpers.cs Fri Jan 11 22:34:18 2019 -0500 +++ b/ServerMonitor/Helpers.cs Sun Feb 10 20:51:26 2019 -0500 @@ -4,6 +4,7 @@ using System.ComponentModel; using System.Drawing; using System.Linq; +using System.Net.NetworkInformation; using System.Text; using System.Windows.Forms; @@ -50,6 +51,7 @@ case CheckStatus.Information: return Resources.info; case CheckStatus.Success: return Resources.pass; case CheckStatus.Running: return Resources.run; + case CheckStatus.Disabled: return Resources.disable; default: return null; } } @@ -57,5 +59,40 @@ public static bool In(this Enum value, params Enum[] values) { return values.Contains(value); } + + // https://stackoverflow.com/a/8345173 + /// <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; + } } }