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
{
/// Resizes the image on a button to fit inside the button's dimensions.
/// A button containing an image.
public static void FormatImageButton(Button button)
{
button.Image = new Bitmap(button.Image, button.Height - 8, button.Height - 8);
}
/// Recursively collects the messages for an exception and its inner exceptions.
/// The parent exception.
/// A string containing the messages for the exception and its inner exceptions.
public static string GetAllMessages(this Exception ex)
{
return "\t" + (ex.InnerException == null ? ex.Message : ex.Message + Environment.NewLine + ex.InnerException.GetAllMessages());
}
/// Gets the value of the DisplayNameAttribute for a type.
/// The type to get the display name of.
/// The type's display name, or the type name if no display name is defined.
public static string GetDisplayName(Type type)
{
return type?.GetAttribute()?.DisplayName ?? type?.Name ?? "null";
}
/// Checks whether a string is null, an empty string, or only contains white space.
/// The string to test.
/// True if the string is null, an empty string, or only contains white space. False otherwise.
public static bool IsNullOrEmpty(this string aString)
{
return aString == null || aString.Trim() == string.Empty;
}
/// Converts all newlines in a string to unix format.
/// The string to convert.
/// The string with all newlines converted to unix format.
public static string ConvertNewlines(this string aString)
{
return aString.Replace("\r\n", "\n").Replace('\r', '\n');
}
/// Gets an attribute on a class.
/// The type of the attribute to return.
/// The type of the class the attribute is on.
/// The attribute, or null if the attribute does not exist on the class.
public static T GetAttribute(this Type type) where T : Attribute
{
return type.GetCustomAttributes(typeof(T), false).SingleOrDefault() as T;
}
/// Gets an image associated with a check status for use in the UI.
/// The check status.
/// The image associated with the check status.
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 Resources.info;
case CheckStatus.Success: return Resources.pass;
case CheckStatus.Running: return Resources.run;
case CheckStatus.Disabled: return Resources.disable;
default: return null;
}
}
/// Gets a program icon associated with a check status.
/// The check status.
/// The program icon associated with the check status.
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;
}
}
/// Returns whether an enum is in a list of values.
/// The value to check.
/// The list of possible values.
/// True if the value was in the list, false otherwise.
public static bool In(this Enum value, params Enum[] values) {
return values.Contains(value);
}
// https://stackoverflow.com/a/8345173
///
/// Indicates whether any network connection is available.
/// Filter connections below a specified speed, as well as virtual network cards.
///
///
/// true if a network connection is available; otherwise, false.
///
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;
}
}
}