get mac address
Published: 9. October 2011 | Updated: 26. October 2011License: Microsoft Public License (MS-PL)
Categories: Windows » Hardware, Network
Tags: C# Hardware Network Windows
Import namespaces
using System.Collections.Generic; using System.Net.NetworkInformation; using System.Text;
Code
public static string[] GetPhysicalAddresses() { var result = new List<string>(); NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface adapter in interfaces) { PhysicalAddress address = adapter.GetPhysicalAddress(); byte[] bytes = address.GetAddressBytes(); if (bytes.Length == 0) continue; var mac = new StringBuilder(); for (int i = 0; i < bytes.Length; i++) { mac.Append(bytes[i].ToString("X2")); if (i != bytes.Length - 1) mac.Append("-"); } result.Add(mac.ToString()); } return result.ToArray(); }
| Send us feedback about this snippet » |





