gpt4 book ai didi

c# - 从默认网关获取mac地址?

转载 作者:太空宇宙 更新时间:2023-11-03 18:48:31 24 4
gpt4 key购买 nike

有没有办法使用 C# 将 mac 地址从默认网关解析出来?

更新我正在使用

var x = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0].GetIPProperties().GatewayAddresses; 

但我觉得我错过了什么。

最佳答案

像这样的东西应该适合你,虽然你可能想添加更多的错误检查:

[DllImport("iphlpapi.dll", ExactSpelling = true)]
public static extern int SendARP(uint destIP, uint srcIP, byte[] macAddress, ref uint macAddressLength);

public static byte[] GetMacAddress(IPAddress address)
{
byte[] mac = new byte[6];
uint len = (uint)mac.Length;
byte[] addressBytes = address.GetAddressBytes();
uint dest = ((uint)addressBytes[3] << 24)
+ ((uint)addressBytes[2] << 16)
+ ((uint)addressBytes[1] << 8)
+ ((uint)addressBytes[0]);
if (SendARP(dest, 0, mac, ref len) != 0)
{
throw new Exception("The ARP request failed.");
}
return mac;
}

关于c# - 从默认网关获取mac地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2135678/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com