Archive for Aprile 22nd, 2008

IsGuid Function

CodeKeep C# Feed Aprile 22nd, 2008

Description: returns bool if a string in the proper format for a GUID, using a regular expression.

Link: http://www.codekeep.net/snippets/a487e226-334f-46d2-9c79-d4414b0c5f3c.aspx

public static bool IsGUID(string expression)
{
    if (expression != null)
    {
        Regex guidRegEx = new Regex(@"^(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$");

        return guidRegEx.IsMatch(expression);
    }
    return false;
}

Button Event Handler

CodeKeep C# Feed Aprile 22nd, 2008

Description: Code behind button event handler. Change the method name.

Link: http://www.codekeep.net/snippets/ca7445e4-4a3e-443b-8fee-84f38c4fdf3b.aspx

        #region event handlers
        protected void ButtonEventHandler(object sender, EventArgs e)
        {
        }
        #endregion event handlers

Retrieve MAC Address

CodeKeep C# Feed Aprile 22nd, 2008

Description: This is sample code to retrieve the MAC address from the current NIC.

Link: http://www.codekeep.net/snippets/052c0e18-a094-4876-9d0e-d661e3d88491.aspx

using System;
// Be sure to add a reference to System.Management for this class to work properly
using System.Management;

namespace MacAddress
{
    class MACAddress
    {
        public string GetMACAddress()
        {
            return GetMACAddress("");
        }

        public string GetMACAddress(string separator)
        {
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = mc.GetInstances();
            string MACAddress = String.Empty;
            foreach (ManagementObject mo in moc)
            {
                if (MACAddress == String.Empty) // only return MAC Address from first card
                {
                    if ((bool)mo["IPEnabled"] == true)
                        MACAddress = mo["MacAddress"].ToString();
                }
                mo.Dispose();
            }
            MACAddress = MACAddress.Replace(":", separator);
            return MACAddress;
        }
    }
}