c# - 如何使用 C# 获取 IP 地址的物理 (MAC) 地址?

标签 c# networking

在 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/

相关文章:

c# - 使用 .NET 进行公钥加密并使用 Java 进行解密

c# - 有没有办法知道函数是否是来自 MethodInfo 的 Expression-bodied Member

sockets - 'socket'系统调用中的“SOCK_RAW”选项

c# - 在此 C# 代码中,符号 "=>"在我的方法之后做什么?

c# - VB6 变体类型到 .NET 类型

c# - 使用 cshtml 和 javascript 动态更改下拉列表

docker - 无法更改Docker构建中的默认worldserver端口

unity-game-engine - 如何在unity Netcode中为GameObject使用ClientRpc(或ServerRpc)?

java - 通过 wifi 连接 Java 套接字?

算法 : Using maximum flow to calculate correct matrix values