C#中从IP中获取本地网络中计算机的MAC地址在我的本地网络上查询机器的MAC地址。我见过很多获取本地机器自己的MAC地址的例子,但没有一个(我发现的)似乎在查询本地网络机器。我知道这样的任务是可能的,因为这个局域网唤醒扫描软件扫描本地IP范围并返回所有机器上的MAC地址/主机名。谁能告诉我从哪里开始尝试编写一个函数来在C#中实现这一点?任何帮助将不胜感激。谢谢编辑:根据下面MarcoMp的评论,使用了ARP表。arpclasspublicstringGetMacAddress(stringipAddress){stringmacAddress=string.Empty;System.Diagnostics.ProcesspProcess=newSystem.Diagnostics.Process();pProcess.StartInfo.FileName="arp";pProcess.StartInfo.Arguments="-a"+ipAddress;pProcess.StartInfo.UseShellExecute=false;pProcess.StartInfo.RedirectStandardOutput=true;pProcess.StartInfo.CreateNoWindow=true;pProcess.Start();字符串strOutput=pProcess.StandardOutput.ReadToEnd();string[]substrings=strOutput.Split('-');if(substrings.Length>=8){macAddress=substrings[3].Substring(Math.Max(0,substrings[3].Length-2))+"-"+substrings[4]+"-"+substrings[5]+"-"+substrings[6]+"-"+substrings[7]+"-"+substrings[8].Substring(0,2);返回mac地址;}else{返回“未找到”;}}很晚编辑:在开源项目iSpy(https://github.com/ispysoftware/iSpy)他们使用这个代码,这是一个更好的publicstaticvoidRefreshARP(){_arpList=newDictionary();_arpList.Clear();尝试{vararpStream=ExecuteCommandLine("arp","-a");//使用前三行for(inti=0;i0){foreach(varndinList){stringmac;ARPList.TryGetValue(nd.IPAddress.ToString(),outmac);nd.MAC=mac;}}}https://github.com/ispysoftware/iSpy/blob/master/Server/NetworkDeviceList.cs更新2较晚,但我认为最好,因为它使用正则表达式来更好地检查精确匹配publicstringgetMacByIp(stringip){varmacIpPairs=GetAllMacAddressesAndIppairs();intindex=macIpPairs.FindIndex(x=>x.IpAddress==ip);如果(索引>=0){返回macIpPairs[索引].MacAddress.ToUpper();}else{返回空值;}}publicListGetAllMacAddressesAndIppairs(){Listmip=newList();System.Diagnostics.ProcesspProcess=newSystem.Diagnostics.Process();pProcess.StartInfo.FileName="arp";pProcess.StartInfo.Arguments="-a";pProcess.StartInfo.UseShellExecute=false;pProcess.StartInfo.RedirectStandardOutput=true;pProcess.StartInfo.CreateNoWindow=true;pProcess.Start();字符串cmdOutput=pProcess.StandardOutput.ReadToEnd();字符串模式=@"(?([0-9]{1,3}.?){4})s*(?([a-f0-9]{2}-?){6})";foreach(在Regex.Matches(cmdOutput,pattern,RegexOptions.IgnoreCase)中匹配m){mip.Add(newMacIpPair(){MacAddress=m.Groups["mac"].Value,IpAddress=m.Groups["ip"]。价值});}返回mip;}公共结构MacIpPair{公共st环Mac地址;公共字符串IP地址;}使用System.Net;使用System.Runtime.InteropServices;[DllImport("iphlpapi.dll",ExactSpelling=true)]publicstaticexternintSendARP(intDestIP,intSrcIP,[Out]byte[]pMacAddr,refintPhyAddrLen);尝试{IPAddresshostIPAddress=IPAddress.Parse("XXX.XXX.XXX.XX");byte[]ab=newbyte[6];intlen=ab.Length,r=SendARP((int)hostIPAddress.Address,0,ab,reflen);Console.WriteLine(BitConverter.ToString(ab,0,6));}catch(Exceptionex){}或使用PC名称try{Tempaddr=System.Net.Dns.GetHostEntry("DESKTOP-xxxxxx");}catch(Exceptionex){}byte[]ab=newbyte[6];intlen=ab.Length,r=SendARP((int)Tempadr.AddressList[1].Address,0,ab,reflen);Console.WriteLine(BitConverter.ToString(ab,0,6));根据上面MarcoMp的评论,使用ARP表arpclass只是一个更好接受的方法Version。以上就是C#学习教程:在C#中从IP获取本机MAC地址共享的所有内容。如果对大家有用,需要进一步了解C#学习教程,希望大家多加关注—publicstringGetMacByIp(stringip){varpairs=this.GetMacIpPairs();foreach(varpairinpairs){if(pair.IpAddress==ip)returnpair.MacAddress;}thrownewException($"无法从ip检索mac地址:{ip}");}publicIEnumerableGetMacIpPairs(){System.Diagnostics.ProcesspProcess=newSystem.Diagnostics.Process();pProcess.StartInfo.FileName="arp";pProcess.StartInfo.Arguments="-a";pProcess.StartInfo.UseShellExecute=false;pProcess.StartInfo.RedirectStandardOutput=true;pProcess.StartInfo.CreateNoWindow=true;pProcess.Start();字符串cmdOutput=pProcess.StandardOutput.ReadToEnd();字符串模式=@"(?([0-9]{1,3}.?){4})s*(?([a-f0-9]{2}-?){6})";foreach(MatchminRegex.Matches(cmdOutput,pattern,RegexOptions.IgnoreCase)){yieldreturnnewMacIpPair(){MacAddress=m.Groups["mac"].Value,IpAddress=m.Groups["ip"].价值e};}}publicstructMacIpPair{publicstringMacAddress;公共字符串IP地址;}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
