C#

Get MAC Address with C#

public string GetLocalIPAddress()
{
	foreach (NetworkInterface NetI in NetworkInterface.GetAllNetworkInterfaces())
	{
		if (NetI.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 ||
			NetI.NetworkInterfaceType == NetworkInterfaceType.Ethernet &&
			NetI.OperationalStatus == OperationalStatus.Up)
		{
			foreach (UnicastIPAddressInformation UniIpAddrInfo in NetI.GetIPProperties().UnicastAddresses)
			{
				if (UniIpAddrInfo.Address.AddressFamily != AddressFamily.InterNetwork ||
					UniIpAddrInfo.AddressPreferredLifetime == UInt32.MaxValue)
					continue;
 
				return UniIpAddrInfo.Address.ToString();
			}
		}
	}
 
	return "Unknown";
}
 
public string GetMacAddress()
{
	foreach (NetworkInterface NetI in NetworkInterface.GetAllNetworkInterfaces())
	{
		if (NetI.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 ||
			NetI.NetworkInterfaceType == NetworkInterfaceType.Ethernet &&
			NetI.OperationalStatus == OperationalStatus.Up)
		{
			bool found = false;
 
			foreach (UnicastIPAddressInformation UniIpAddrInfo in NetI.GetIPProperties().UnicastAddresses)
			{
				if (UniIpAddrInfo.Address.AddressFamily != AddressFamily.InterNetwork ||
					UniIpAddrInfo.AddressPreferredLifetime == UInt32.MaxValue)
					continue;
 
				found = (UniIpAddrInfo.Address.ToString() == GetLocalIPAddress());
			}
 
			if (found)
			{
				string macAddr = NetI.GetPhysicalAddress().ToString();
				return (macAddr.Length != 12) ? "00:00:00:00:00:00"
					: Regex.Replace(macAddr, "(.{2})(.{2})(.{2})(.{2})(.{2})(.{2})", "$1:$2:$3:$4:$5:$6");
			}
		}
	}
 
	return "Unknown";
}