C # get MAC address

Source: Internet
Author: User

/********************************************************************** * 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.