1stringGetmac ()2 {3 stringMac ="";4Mac =Getmacaddressbysendarp ();5 returnmac;6 }7[DllImport ("Iphlpapi.dll")]8 Static extern intSendarp (Int32 destip, Int32 Srcip,refInt64 Macaddr,refInt32 Phyaddrlen);9 /// <summary> Ten ///Sendarp get MAC address One /// </summary> A /// <returns></returns> - Public stringGetmacaddressbysendarp () - { theStringBuilder Strreturn =NewStringBuilder (); - Try - { -System.Net.IPHostEntry tempaddr =(System.Net.IPHostEntry) Dns.gethostbyname (Dns.gethostname ()); +system.net.ipaddress[] Tempad =tempaddr.addresslist; -Int32 Remote = (int) tempad[0]. Address; +Int64 Macinfo =NewInt64 (); AInt32 length =6; atSendarp (remote,0,refMacinfo,reflength); - stringtemp = System.Convert.ToString (Macinfo, -). PadLeft ( A,'0'). ToUpper (); - intx = A; - for(inti =0; I <6; i++) - { - if(i = =5) {Strreturn.append (temp. Substring (X-2,2)); } in Else{Strreturn.append (temp. Substring (X-2,2) +":"); } -X-=2; to } + returnstrreturn.tostring (); - } the Catch * { $ return "";Panax Notoginseng } -}
The above code can directly call the Getmac () function to get the MAC address of the computer
Unity Network Communication (i) Get the MAC address of the computer