view ServerMonitor/Helpers.cs @ 22:48044d9ac000

Change default check interval from 5 seconds to 5 minutes.
author Brad Greco <>
date Thu, 30 May 2019 21:40:02 -0400
parents 68d7834dc28e
children 7645122aa7a9
line wrap: on
line source

´╗┐using ServerMonitorApp.Properties;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Net.NetworkInformation;
using System.Windows.Forms;

namespace ServerMonitorApp
    static class Helpers
        /// <summary>Resizes the image on a button to fit inside the button's dimensions.</summary>
        /// <param name="button">A button containing an image.</param>
        public static void FormatImageButton(Button button)
            button.Image = new Bitmap(button.Image, button.Height - 8, button.Height - 8);

        /// <summary>Recursively collects the messages for an exception and its inner exceptions.</summary>
        /// <param name="ex">The parent exception.</param>
        /// <returns>A string containing the messages for the exception and its inner exceptions.</returns>
        public static string GetAllMessages(this Exception ex)
            return "\t" + (ex.InnerException == null ? ex.Message : ex.Message + Environment.NewLine + ex.InnerException.GetAllMessages());

        /// <summary>Gets the value of the DisplayNameAttribute for a type.</summary>
        /// <param name="type">The type to get the display name of.</param>
        /// <returns>The type's display name, or the type name if no display name is defined.</returns>
        public static string GetDisplayName(Type type)
            return type?.GetAttribute<DisplayNameAttribute>()?.DisplayName ?? type?.Name ?? "null";

        /// <summary>Checks whether a string is null, an empty string, or only contains white space.</summary>
        /// <param name="aString">The string to test.</param>
        /// <returns>True if the string is null, an empty string, or only contains white space. False otherwise.</returns>
        public static bool IsNullOrEmpty(this string aString)
            return aString == null || aString.Trim() == string.Empty;

        /// <summary>Converts all newlines in a string to unix format.</summary>
        /// <param name="aString">The string to convert.</param>
        /// <returns>The string with all newlines converted to unix format.</returns>
        public static string ConvertNewlines(this string aString)
            return aString.Replace("\r\n", "\n").Replace('\r', '\n');

        /// <summary>Gets an attribute on a class.</summary>
        /// <typeparam name="T">The type of the attribute to return.</typeparam>
        /// <param name="type">The type of the class the attribute is on.</param>
        /// <returns>The attribute, or null if the attribute does not exist on the class.</returns>
        public static T GetAttribute<T>(this Type type) where T : Attribute
            return type.GetCustomAttributes(typeof(T), false).SingleOrDefault() as T;

        /// <summary>Gets an image associated with a check status for use in the UI.</summary>
        /// <param name="checkStatus">The check status.</param>
        /// <returns>The image associated with the check status.</returns>
        public static Image GetImage(this CheckStatus checkStatus)
            switch (checkStatus)
                case CheckStatus.Error: return Resources.error;
                case CheckStatus.Warning: return Resources.warning;
                case CheckStatus.Information: return;
                case CheckStatus.Success: return Resources.pass;
                case CheckStatus.Running: return;
                case CheckStatus.Disabled: return Resources.disable;
                default: return null;

        /// <summary>Gets a program icon associated with a check status.</summary>
        /// <param name="checkStatus">The check status.</param>
        /// <returns>The program icon associated with the check status.</returns>
        public static Icon GetIcon(this CheckStatus checkStatus)
            switch (checkStatus)
                case CheckStatus.Error: return Resources.icon_error;
                case CheckStatus.Warning: return Resources.icon_warning;
                case CheckStatus.Information: return Resources.icon_info;
                default: return Resources.icon;

        /// <summary>Returns whether an enum is in a list of values.</summary>
        /// <param name="value">The value to check.</param>
        /// <param name="values">The list of possible values.</param>
        /// <returns>True if the value was in the list, false otherwise.</returns>
        public static bool In(this Enum value, params Enum[] values) {
            return values.Contains(value);

        /// <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))

                // discard virtual cards (virtual box, virtual pc, etc.)
                if ((ni.Description.IndexOf("virtual", StringComparison.OrdinalIgnoreCase) >= 0) ||
                    (ni.Name.IndexOf("virtual", StringComparison.OrdinalIgnoreCase) >= 0))

                // discard "Microsoft Loopback Adapter", it will not show as NetworkInterfaceType.Loopback but as Ethernet Card.
                if (ni.Description.Equals("Microsoft Loopback Adapter", StringComparison.OrdinalIgnoreCase))

                return true;
            return false;