您好,欢迎访问一九零五行业门户网

C# 在指定的IP地址中获得一个设备的MAC(物理)地址

using system.net; using system.net.networkinformation; /// /// holds utilities for working with networks, ethernet, etc. /// public static class networkutils { // http://www.codeproject.com/kb/ip/host_info_within_network.aspx [system.runtime.interopservices.dllimport(iphlpapi.dll, exactspelling = true)] static extern int sendarp(int destip, int srcip, byte[] pmacaddr, ref int phyaddrlen); /// /// gets the mac address () associated with the specified ip. /// /// the remote ip address. /// the remote machine's mac address. public static physicaladdress getmacaddress(ipaddress ipaddress) { const int macaddresslength = 6; int length = macaddresslength; var macbytes = new byte[macaddresslength]; sendarp(bitconverter.toint32(ipaddress.getaddressbytes(), 0), 0, macbytes, ref length); return new physicaladdress(macbytes); } } [testclass()] public class networkutilstests { [testmethod()] public void getmacaddress_broadcastip_nonzeromac() { ipaddress ipaddress = ipaddress.broadcast; physicaladdress actual = networkutils.getmacaddress (ipaddress); console.writeline(actual.tostring()); assert.arenotequal(physicaladdress.none, actual); } }
其它类似信息

推荐信息