/********************************************************************** * C # get MAC Address * Description: * Get the MAC address of native in C #, the article provides two references, one is capable of all Mac * addresses, and one is to get the first MAC address. * 2016-12-9 Shenzhen Nanshan Ping Shan village Zengjianfeng ************************************************************** *******/First, reference documents:1. Reliable method toGetMachine's MAC address in C #http//Stackoverflow.com/questions/850650/reliable-method-to-get-machines-mac-address-in-c-sharpSecond, the solution:usingSystem; usingSystem.Collections.Generic; usingSystem.Text; usingSystem.Net.NetworkInformation; namespaceLocaldetecttest {classNetTools {/// <summary> ///Finds The MAC address of the NIC with maximum speed. /// </summary> /// <returns>The MAC address.</returns> Public Static voidprintallmacaddress () {Const intMin_mac_addr_length = A; stringMacAddress =string. Empty; LongMaxspeed =-1; foreach(NetworkInterface NICinchnetworkinterface.getallnetworkinterfaces ()) {Console.WriteLine ("Name:"+ NIC. Name +"Found MAC Address:"+ NIC. Getphysicaladdress () +"Type:"+Nic.networkinterfacetype); stringTempmac =NIC. Getphysicaladdress (). ToString (); if(NIC. Speed > Maxspeed &&!string. IsNullOrEmpty (TEMPMAC) &&Tempmac.length>=min_mac_addr_length) {Console.WriteLine ("New Max speed ="+ NIC. Speed +", MAC:"+Tempmac); Maxspeed=NIC. Speed; MacAddress=Tempmac; } } //return macAddress; } /// <summary> ///Finds The MAC address of the first operation NIC found. /// </summary> /// <returns>The MAC address.</returns> Public Static stringgetfirstmacaddress () {stringMacaddresses =string. Empty; foreach(NetworkInterface NICinchnetworkinterface.getallnetworkinterfaces ()) { if(NIC. Operationalstatus = =operationalstatus.up) {macaddresses+=NIC. Getphysicaladdress (). ToString (); Console.WriteLine (macaddresses); Break; } } returnmacaddresses; } } }
C # get MAC address