在 C# 中,我想执行以下等效操作:
arp -a |findstr 192.168.1.254
或者,答案可以调用 SendARP函数并得到结果。
这将允许我的应用程序执行一些需要 MAC 地址的其他处理。
最佳答案
SendARP P/Invoke 是这样的:
[DllImport("iphlpapi.dll", ExactSpelling=true)]
public static extern int SendARP( int destIp, int srcIP, byte[] macAddr, ref uint physicalAddrLen );
PInvoke.NET有这个例子:
IPAddress dst = IPAddress.Parse("192.168.2.1"); // the destination IP address
byte[] macAddr = new byte[6];
uint macAddrLen = (uint)macAddr.Length;
if (SendARP(BitConverter.ToInt32(dst.GetAddressBytes(), 0), 0, macAddr, ref macAddrLen) != 0)
throw new InvalidOperationException("SendARP failed.");
string[] str = new string[(int)macAddrLen];
for (int i=0; i<macAddrLen; i++)
str[i] = macAddr[i].ToString("x2");
Console.WriteLine(string.Join(":", str));
关于c# - 如何使用 C# 获取 IP 地址的物理 (MAC) 地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/187894/