标签:mac地址
C#实现获取电脑的MAC地址
/// <summary> /// Microsoft提供的WMI(Windows Management Instrumentation)中提供了一个 /// Win32_NetworkAdapterConfiguration的类,定义了获取MAC地址的方法,因此只要构造一个定义为Win32_NetworkAdapter /// Configuration的WMI类,从中取出MAC地址的值即可。需要手动添加System.Management链接库 /// </summary> /// <returns>string类型的Mac地址</returns> public string GetMacAddress() { string text = string.Empty; string result; try { ManagementClass managementClass = new ManagementClass("Win32_NetWorkAdapterConfiguration"); ManagementObjectCollection instances = managementClass.GetInstances(); using (ManagementObjectCollection.ManagementObjectEnumerator enumerator = instances.GetEnumerator()) { while (enumerator.MoveNext()) { ManagementObject managementObject = (ManagementObject)enumerator.Current; if (Convert.ToBoolean(managementObject["IPEnabled"])) { text = managementObject["MacAddress"].ToString().Replace(‘:‘, ‘-‘); } managementObject.Dispose(); } } if (string.IsNullOrEmpty(text)) { throw new Exception("The Mac address does not empty"); } result = text; } catch(Exception ex) { throw new Exception("Get mac address error occured:" + ex.Message); } return result; }
本文出自 “花开花落” 博客,谢绝转载!
标签:mac地址
原文地址:http://020618.blog.51cto.com/6098149/1655795