Mostrando entradas con la etiqueta C#. Mostrar todas las entradas
Mostrando entradas con la etiqueta C#. Mostrar todas las entradas

Palindrome Checker

bool IsPalindrome(string phrase = "Yo dono rosas, Oro no doy")
{
    string text = Regex.Replace(phrase.ToLower(), "[^A-Za-z0-9À-ü]", "");
    for (int index = 0; index < text.Length / 2; index++)
    {
        if (text[index] != text[text.Length - 1 - index])
        {
            return false;
        }
    }
    return true;
}

WPF Color Generator

namespace SenenFernandez.SmartExtensionMethods
{
    using System;
    using System.Windows.Media;

    public class SmartColorGenerator
    {
        private readonly Random random;
        private static readonly object sync = new object();
        private static SmartColorGenerator instance;

        public static SmartColorGenerator Instance
        {
            get
            {
                lock (sync)
                {
                    return instance ?? (instance = new SmartColorGenerator());
                }
            }
        }

        public SmartColorGenerator(int seed = 255)
        {
            random = new Random(seed);
        }

        public SolidColorBrush GetBrush(double opacity = 1.0)
        {
            var brush = new SolidColorBrush(GetColor(opacity));
            brush.Freeze();
            return brush;
        }

        public Color GetColor(double opacity = 1.0)
        {
            int alpha = (int)
                (255*(opacity > 1.0 ? 1 : opacity < 0 ? 0 : opacity));
            var color = new Color
            {
                A = (byte) (alpha),
                R = GetNext(),
                B = GetNext(),
                G = GetNext()
            };
            return color;
        }

        private byte GetNext()
        {
            return (byte) (random.Next(127) + 128);
        }
    }
}

WMI Query to Get IP Address

var query = new System.Management.SelectQuery(
    @"SELECT *
    FROM Win32_NetworkAdapterConfiguration 
    WHERE IPEnabled = TRUE 
    OR DHCPEnabled = TRUE");
var searcher = new System.Management.ManagementObjectSearcher(query);
var netCards = searcher.Get();
foreach(var netCard in netCards)
{
    var ips = netCard["IPAddress"] as string[];
    if (ips != null)
    {
        System.Console.WriteLine("{0}\t{1}"
            , netCard["Description"]
            , netCard["MACAddress"]);
        foreach(var ip in ips)
        {
            System.Console.WriteLine("\t{0}", ip);
        }
    }
}