view ServerMonitor/Helpers.cs @ 2:453ecc1ed9ea

Disk space check
author Brad Greco <brad@bgreco.net>
date Sun, 06 Jan 2019 20:49:08 -0500
parents 3e1a2131f897
children 3142e52cbe69
line wrap: on
line source

´╗┐using ServerMonitorApp.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ServerMonitorApp
{
    static class Helpers
    {
        public static void FormatImageButton(Button button)
        {
            button.Image = new Bitmap(button.Image, button.Height - 8, button.Height - 8);
        }

        public static string GetAllMessages(this Exception ex)
        {
            return "\t" + (ex.InnerException == null ? ex.Message : ex.Message + Environment.NewLine + ex.InnerException.GetAllMessages());
        }

        public static string GetDisplayName(Type type)
        {
            return type?.GetAttribute<DisplayNameAttribute>()?.DisplayName ?? type?.Name ?? "null";
        }

        public static bool IsNullOrEmpty(this string aString)
        {
            return aString == null || aString.Trim() == string.Empty;
        }

        public static string ConvertNewlines(this string aString)
        {
            return aString.Replace("\r\n", "\n").Replace('\r', '\n');
        }

        public static T GetAttribute<T>(this Type type) where T : Attribute
        {
            return type.GetCustomAttributes(typeof(T), false).SingleOrDefault() as T;
        }

        public static Image GetIcon(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;
                default: return null;
            }
        }

        public static bool In(this Enum value, params Enum[] values) {
            return values.Contains(value);
        }
    }
}